Skip to content

Autonomous AI agent for bug detection, diagnosis, and automated fixing. Multi-source error detection with AI-powered healing and zero API costs.

Notifications You must be signed in to change notification settings

WilBtc/bug-hunter-agent

Repository files navigation

Bug Hunter Agent - Automated Error Detection & Bug Fixing

Purpose: 24/7 automated error detection, AI-powered diagnosis, and self-healing Created: October 26, 2025 Version: 1.0.0 Status: ✅ PRODUCTION READY


Overview

The Bug Hunter Agent is an autonomous system that continuously monitors your entire INSA platform for errors, automatically diagnoses issues using AI, attempts automated fixes, and creates GitHub issues for complex bugs that require human intervention.

Key Features

  • 24/7 Monitoring - Continuous error detection across all services
  • Multi-Source Detection - Logs, services, containers, application errors
  • AI-Powered Diagnosis - Claude Code integration for intelligent analysis
  • Automated Fixing - Service restarts, config changes, dependency fixes
  • Learning System - Builds database of successful fix patterns
  • GitHub Integration - Auto-creates issues for unresolvable bugs
  • SQLite Database - Persistent tracking of all bugs and fixes
  • Zero API Costs - Uses local Claude Code subprocess

Architecture

┌──────────────────────────────────────────────────────────┐
│              BUG HUNTER WORKFLOW                          │
└──────────────────────────────────────────────────────────┘
                          │
                          ▼
         ┌────────────────────────────────┐
         │   1. ERROR DETECTION           │
         │   • Scan logs every 5 minutes  │
         │   • Monitor systemd services   │
         │   • Check Docker containers    │
         │   • Track application errors   │
         └────────────────┬───────────────┘
                          │
                          ▼
         ┌────────────────────────────────┐
         │   2. INTELLIGENT TRIAGE        │
         │   • Calculate bug hash         │
         │   • Deduplicate known issues   │
         │   • Classify severity          │
         │   • Extract stack traces       │
         └────────────────┬───────────────┘
                          │
                          ▼
         ┌────────────────────────────────┐
         │   3. AI DIAGNOSIS              │
         │   • Check fix pattern DB       │
         │   • Claude Code analysis       │
         │   • Root cause identification  │
         │   • Fix strategy selection     │
         └────────────────┬───────────────┘
                          │
                          ▼
         ┌────────────────────────────────┐
         │   4. AUTOMATED FIXING          │
         │   • Service restart            │
         │   • Container restart          │
         │   • Config adjustment          │
         │   • Dependency resolution      │
         └────────────────┬───────────────┘
                          │
                          ▼
         ┌────────────────────────────────┐
         │   5. VERIFICATION              │
         │   • Check fix success          │
         │   • Monitor for regression     │
         │   • Update statistics          │
         │   • Learn from outcome         │
         └────────────────┬───────────────┘
                          │
                     ┌────┴────┐
                     │         │
                Fixed │         │ Failed
                     │         │
                     ▼         ▼
         ┌──────────────┐  ┌─────────────────┐
         │ 6a. SUCCESS  │  │ 6b. ESCALATION  │
         │ • Mark fixed │  │ • Create GitHub │
         │ • Save       │  │   issue         │
         │   pattern    │  │ • Alert team    │
         │ • Update DB  │  │ • Track attempt │
         └──────────────┘  └─────────────────┘

MCP Tools (7 Available)

1. scan_for_bugs

Purpose: Scan logs, services, and containers for errors

Parameters:

  • hours (int): Hours to look back (default: 1)
  • log_files (array): Specific log files to scan
  • include_services (bool): Check systemd services (default: true)
  • include_containers (bool): Check Docker containers (default: true)

Example:

{
  "hours": 2,
  "log_files": ["/var/log/syslog", "/tmp/crm-backend.log"],
  "include_services": true,
  "include_containers": true
}

Returns:

  • Total errors found
  • New bugs added to database
  • Errors grouped by type
  • Preview of first 10 errors

2. list_bugs

Purpose: List detected bugs from database

Parameters:

  • status (enum): Filter by status (detected, attempted, fixed, ignored)
  • limit (int): Max results (default: 50)

Example:

{
  "status": "detected",
  "limit": 20
}

3. diagnose_bug

Purpose: AI-powered diagnosis of specific bug

Parameters:

  • bug_id (int): Bug ID from database (required)

Returns:

  • Root cause analysis
  • Recommended fix type
  • Specific fix steps
  • Risk level assessment

4. auto_fix_bug

Purpose: Attempt automated fix for bug

Parameters:

  • bug_id (int): Bug ID to fix (required)
  • force (bool): Force fix even if risky (default: false)

Fix Types:

  • service_restart - Restart systemd service
  • container_restart - Restart Docker container
  • config_change - Modify configuration
  • dependency_fix - Resolve dependencies

5. create_github_issue

Purpose: Create GitHub issue for complex bugs

Parameters:

  • bug_id (int): Bug ID to create issue for (required)

Integration: Uses GitHub Agent MCP server

6. get_bug_stats

Purpose: Get bug statistics and trends

Returns:

  • Total bugs detected
  • Fixed bugs count
  • Auto-fix success rate
  • Bugs by type breakdown
  • Time series trends

7. learn_fix_pattern

Purpose: Add new fix pattern to learning database

Parameters:

  • error_pattern (string): Error pattern to match (required)
  • fix_template (string): Fix template to apply (required)

Database Schema

bugs table

- id: INTEGER PRIMARY KEY
- bug_hash: TEXT UNIQUE (deduplication)
- title: TEXT
- description: TEXT
- error_type: TEXT (error, critical, exception, etc.)
- stack_trace: TEXT
- source_file: TEXT
- line_number: INTEGER
- service: TEXT
- severity: TEXT (low, medium, high, critical)
- status: TEXT (detected, attempted, fixed, ignored)
- detected_at: TIMESTAMP
- fixed_at: TIMESTAMP
- fix_attempts: INTEGER
- auto_fixed: BOOLEAN

fixes table

- id: INTEGER PRIMARY KEY
- bug_id: INTEGER (FK to bugs)
- fix_type: TEXT
- fix_description: TEXT
- fix_code: TEXT
- success: BOOLEAN
- applied_at: TIMESTAMP
- verification_result: TEXT

fix_patterns table (Learning System)

- id: INTEGER PRIMARY KEY
- error_pattern: TEXT
- fix_template: TEXT
- success_count: INTEGER
- failure_count: INTEGER
- confidence: REAL (0.0 to 1.0)
- last_used: TIMESTAMP
- created_at: TIMESTAMP

github_issues table

- id: INTEGER PRIMARY KEY
- bug_id: INTEGER (FK to bugs)
- issue_number: INTEGER
- issue_url: TEXT
- created_at: TIMESTAMP

Database Location: /var/lib/bug-hunter/bugs.db


Error Detection Sources

1. Log Files

  • /var/log/syslog - System logs
  • /tmp/crm-backend.log - CRM backend
  • /tmp/insa-crm.log - INSA CRM core
  • /var/log/defectdojo_remediation_agent.log - DefectDojo
  • Custom application logs

Detection Patterns:

  • ERROR: - Standard error logging
  • CRITICAL: - Critical failures
  • Exception: - Python exceptions
  • Traceback (most recent call last): - Stack traces
  • fatal: - Fatal errors
  • panic: - Go panics

2. Systemd Services

  • Checks systemctl list-units --state=failed
  • Detects service crashes and failures
  • Monitors 20+ INSA services

3. Docker Containers

  • Checks docker ps -a --filter status=exited
  • Detects abnormal container exits
  • Tracks restart loops

4. Application Errors (Future)

  • Exception tracking middleware
  • API error monitoring
  • Frontend error logging

Automated Fixing Capabilities

Service Restart

# Automatically restarts failed systemd services
sudo systemctl restart <service-name>

Safety: Only restarts if service has not failed >3 times in last hour

Container Restart

# Automatically restarts crashed containers
docker restart <container-name>

Safety: Checks container health before declaring success

Config Adjustment

  • Fixes common misconfigurations
  • Reverts bad changes
  • Validates before applying

Dependency Resolution

  • Reinstalls missing Python packages
  • Updates npm dependencies
  • Rebuilds containers if needed

Learning System

The Bug Hunter uses a self-improving AI that learns from every fix:

How It Learns

  1. Pattern Recognition

    • Each bug creates unique hash: error_type:message_hash
    • Tracks which fixes work for which errors
    • Builds confidence scores over time
  2. Success Tracking

    Confidence = success_count / (success_count + failure_count)
    
    • Patterns with >70% confidence are auto-applied
    • Patterns <30% confidence require human review
  3. Pattern Evolution

    • High-confidence patterns used first
    • Low-confidence patterns deprecated
    • New patterns created from manual fixes

Example Pattern

{
  "error_pattern": "container_crashed",
  "fix_template": "docker restart {container}",
  "success_count": 45,
  "failure_count": 3,
  "confidence": 0.94
}

Integration with Existing Systems

GitHub Agent Integration

  • Creates issues for bugs that cannot be auto-fixed
  • Includes full diagnostic information
  • Tracks fix attempts and outcomes
  • Links bug ID to GitHub issue number

Healing Agent Integration

  • Shares fix patterns and success data
  • Coordinates service-level healing
  • Avoids duplicate fix attempts
  • Combines AI insights

Platform Admin Integration

  • Uses platform health checks
  • Triggers service restarts
  • Validates fix success
  • Reports metrics

DefectDojo Integration

  • Creates findings for security-related bugs
  • Tags with severity levels
  • Tracks remediation status

24/7 Autonomous Operation

The Bug Hunter runs as a systemd service for continuous monitoring:

[Unit]
Description=Bug Hunter - Automated Error Detection & Fixing
After=network.target

[Service]
Type=simple
User=wil
WorkingDirectory=/home/wil/bug-hunter-agent
ExecStart=/home/wil/bug-hunter-agent/bug_hunter_daemon.py
Restart=always
RestartSec=60

[Install]
WantedBy=multi-user.target

Schedule:

  • Every 5 minutes: Scan for new errors
  • Every 15 minutes: Attempt fixes for detected bugs
  • Every hour: Generate statistics and trends
  • Daily: Create GitHub issues for persistent bugs
  • Weekly: Retrain fix pattern confidence scores

Security & Safety

Safety Checks

  1. Rate Limiting

    • Max 3 fix attempts per bug
    • Max 1 service restart per hour
    • Max 5 container restarts per hour
  2. Rollback Protection

    • Backs up configs before changes
    • Monitors for regression after fixes
    • Auto-rollback if fix makes things worse
  3. Severity Thresholds

    • Critical errors: Auto-fix only known patterns
    • High errors: Attempt fix with verification
    • Medium errors: Auto-fix freely
    • Low errors: Log only
  4. Human Override

    • Can mark bugs as "ignored"
    • Can force manual review
    • Can disable auto-fix per bug type

Permissions

  • Requires sudo for service restarts
  • Docker access for container operations
  • File write for logs and database
  • GitHub token for issue creation

Audit Trail

  • Every action logged to database
  • Full stack trace preserved
  • Fix attempts timestamped
  • GitHub issues linked

Performance Metrics

Detection Speed

  • Log scan: ~2 seconds per 1000 lines
  • Service check: ~100ms
  • Container check: ~200ms
  • Total scan cycle: <5 seconds

Fix Success Rates (Expected)

  • Service restart: 95% success
  • Container restart: 90% success
  • Config changes: 70% success
  • Dependency fixes: 60% success
  • Overall auto-fix: 80% success

Resource Usage

  • Memory: ~50MB (including database)
  • CPU: <5% (during scans)
  • Disk: ~100MB (database + logs)
  • Network: Minimal (GitHub API only)

Getting Started

1. Installation

cd ~/mcp-servers/bug-hunter
python3 -m venv venv
source venv/bin/activate
pip install -r requirements.txt

2. Database Setup

# Database will auto-create on first run
# Location: /var/lib/bug-hunter/bugs.db
sudo mkdir -p /var/lib/bug-hunter
sudo chown $USER:$USER /var/lib/bug-hunter

3. MCP Configuration

Add to ~/.mcp.json:

{
  "bug-hunter": {
    "transport": "stdio",
    "command": "/home/wil/mcp-servers/bug-hunter/venv/bin/python",
    "args": ["/home/wil/mcp-servers/bug-hunter/server.py"],
    "env": {
      "PYTHONDONTWRITEBYTECODE": "1",
      "PYTHONUNBUFFERED": "1"
    },
    "_description": "Bug Hunter - Automated error detection and bug fixing with AI diagnosis"
  }
}

4. Test Scan

# In Claude Code
"Scan for bugs in the last hour"

5. Deploy Daemon (Optional)

# See DEPLOYMENT_GUIDE.md for systemd service setup

Usage Examples

Scan for Recent Errors

"Scan for bugs in the last 2 hours"
"Check all services for errors"
"Find errors in CRM logs"

Review Bugs

"List all detected bugs"
"Show me bugs that haven't been fixed"
"Get bug statistics"

Fix Bugs

"Auto-fix bug #42"
"Try to fix all detected bugs"
"Diagnose bug #15"

GitHub Integration

"Create GitHub issue for bug #10"
"Show bugs that need manual review"

Roadmap

Version 1.0 (Current - October 26, 2025):

  • ✅ Multi-source error detection
  • ✅ SQLite persistence
  • ✅ Basic automated fixes
  • ✅ Learning system foundation
  • ✅ GitHub integration ready

Version 1.1 (Q4 2025):

  • 🔄 Full Claude Code AI integration
  • 🔄 Advanced fix strategies
  • 🔄 Predictive error detection
  • 🔄 Custom fix templates
  • 🔄 Slack/email notifications

Version 2.0 (Q1 2026):

  • 🔄 Multi-node deployment
  • 🔄 Real-time WebSocket monitoring
  • 🔄 ML-based error prediction
  • 🔄 Auto-generated fix PRs
  • 🔄 Integration testing before fixes

Troubleshooting

"Permission denied" errors

# Grant sudo access for service restarts
sudo visudo
# Add: wil ALL=(ALL) NOPASSWD: /bin/systemctl restart *

Database locked

# Check for stale connections
lsof /var/lib/bug-hunter/bugs.db
# Kill if necessary

No bugs detected

  • Check log file permissions
  • Verify log file paths exist
  • Ensure services are actually failing
  • Try: scan_for_bugs with hours: 24

Support

  • Created by: Insa Automation Corp
  • Contact: w.aroca@insaing.com
  • Documentation: This file + code comments
  • Database: SQLite at /var/lib/bug-hunter/bugs.db

Made by Insa Automation Corp for OpSec

About

Autonomous AI agent for bug detection, diagnosis, and automated fixing. Multi-source error detection with AI-powered healing and zero API costs.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages