AGENTS.md

# AshProfiler: Performance Optimization Agent for Elixir Ash Framework

**AshProfiler** is a specialized performance analysis and optimization toolkit designed specifically for [Ash Framework](https://ash-hq.org/) applications. It acts as an intelligent agent to identify performance bottlenecks, analyze DSL complexity, and provide actionable recommendations for improving your Ash applications.

## 🎯 Core Mission

AshProfiler serves as your **Performance Optimization Agent**, automatically analyzing your Ash codebase to:

- **Detect Compilation Bottlenecks**: Identify slow-compiling resources and domains
- **Analyze DSL Complexity**: Score the complexity of your Ash DSL patterns
- **Container Optimization**: Provide specialized analysis for containerized deployments
- **Generate Actionable Reports**: Deliver concrete optimization recommendations

## 🚀 Agent Capabilities

### 1. **DSL Complexity Analysis Agent**
Automatically scores and categorizes the complexity of your Ash resources:

```elixir
# Analyze complexity across all domains
AshProfiler.analyze()

# Focus on specific high-impact domains
AshProfiler.analyze(domains: [MyApp.CoreDomain, MyApp.UserDomain])
```

**Scoring System:**
- **Attributes**: Base (1pt), Computed (3pts), Constraints (+1pt each)
- **Relationships**: Base (2pts), Many-to-many (+5pts), Through (+3pts) 
- **Policies**: Base (5pts), Complex expressions (variable), Bypasses (2pts)
- **Actions**: Base (1pt) + Changes (2pts each) + Validations (1pt each)

### 2. **Container Performance Agent**
Specialized analysis for containerized environments:

```bash
# Container-optimized analysis
mix ash_profiler --container-mode --threshold 50
```

**Container-Specific Insights:**
- Memory allocation recommendations
- CPU scheduler optimization
- Multi-stage Docker build suggestions
- Erlang VM tuning for containers

### 3. **Compilation Performance Agent**
Tracks and optimizes compilation performance:

```elixir
# Enable compilation tracking
export ASH_DISABLE_COMPILE_DEPENDENCY_TRACKING=true

# Analyze compilation bottlenecks
AshProfiler.analyze(include_optimizations: true)
```

### 4. **Report Generation Agent**
Multi-format reporting for different use cases:

```elixir
# Console output for development
AshProfiler.analyze(output: :console)

# JSON for CI/CD integration  
AshProfiler.analyze(output: :json, file: "metrics.json")

# HTML for stakeholder reports
AshProfiler.analyze(output: :html, file: "performance_report.html")
```

## 📊 Performance Metrics & Scoring

### Complexity Severity Levels
- **🟢 Low (< 50)**: Well-optimized resource
- **🟡 Medium (50-100)**: Moderate complexity
- **🟠 High (100-150)**: Review recommended
- **🔴 Critical (> 150)**: Optimization needed

### Real-World Impact
Based on production optimizations achieving **98.2% performance improvements**:

```bash
# Environment optimizations
export ELIXIR_ERL_OPTIONS="+sbwt none +sbwtdcpu none +sbwtdio none"
export ERL_FLAGS="+S 4:4 +P 1048576"
```

## 🔧 Integration Patterns

### Development Workflow
```bash
# Quick health check
mix ash_profiler

# Detailed analysis with thresholds
mix ash_profiler --output html --file report.html --threshold 80
```

### CI/CD Integration
```yaml
# GitHub Actions example
- name: Ash Performance Analysis
  run: |
    mix ash_profiler --output json --file metrics.json --threshold 100
    # Fail build if complexity exceeds threshold
    mix ash_profiler --threshold 100 || exit 1
```

### Production Monitoring
```elixir
# Scheduled performance audits
defmodule MyApp.PerformanceAudit do
  def weekly_audit do
    AshProfiler.analyze(
      output: :json,
      file: "weekly_performance_#{Date.utc_today()}.json",
      include_optimizations: true
    )
  end
end
```

## 🎯 Optimization Recommendations Engine

AshProfiler's AI-like recommendation system provides targeted suggestions:

### Policy Optimizations
- Extract complex expressions to computed attributes
- Simplify `authorize_if` conditions
- Implement policy composition patterns

### Relationship Optimizations  
- Move complex relationships to separate resources
- Use manual relationships for complex queries
- Optimize data layer interactions

### Domain Architecture
- Domain splitting recommendations for large domains
- Resource organization improvements
- Compilation performance optimizations

## 🌟 Community Impact

### For Library Authors
- **Benchmark Your DSL Patterns**: Understand the performance impact of your DSL designs
- **Optimization Guidelines**: Provide users with concrete performance recommendations
- **Container Compatibility**: Ensure your libraries work efficiently in containerized environments

### For Application Developers
- **Performance Budget Management**: Track complexity growth over time
- **Refactoring Guidance**: Identify high-impact optimization opportunities
- **Team Alignment**: Share performance insights across development teams

### For DevOps Teams
- **Container Optimization**: Specialized recommendations for Docker/Kubernetes deployments
- **Build Performance**: Optimize CI/CD pipeline compilation times
- **Production Monitoring**: Continuous performance health monitoring

## 📈 Success Stories

### Case Study: 98.2% Performance Improvement
A production Ash application achieved dramatic performance improvements through:

1. **Policy Simplification**: Reduced complex policy expressions
2. **Relationship Optimization**: Restructured many-to-many relationships  
3. **Container Tuning**: Applied Erlang VM optimizations
4. **Compilation Caching**: Implemented multi-stage Docker builds

**Results**: Compilation time reduced from 120s to 2.1s in containerized environments.

## 🚀 Getting Started

### Installation
```elixir
def deps do
  [
    {:ash_profiler, "~> 0.1.0"}
  ]
end
```

### Quick Start
```elixir
# Instant analysis
AshProfiler.analyze()

# Comprehensive report
AshProfiler.analyze(
  output: :html,
  file: "ash_performance.html",
  threshold: 50,
  include_optimizations: true
)
```

### Command Line
```bash
# Basic profiling
mix ash_profiler

# Production-ready analysis
mix ash_profiler --output json --file metrics.json --container-mode
```

## 🤝 Community Contribution

AshProfiler is designed to evolve with the Ash ecosystem:

- **Performance Patterns**: Share common optimization patterns
- **Container Recipes**: Contribute Docker optimization strategies  
- **Benchmark Data**: Help establish community performance baselines
- **Custom Analyzers**: Extend with domain-specific analysis capabilities

## 📚 Resources

- **Documentation**: Comprehensive API and usage examples
- **Performance Guide**: Best practices for Ash application optimization
- **Container Handbook**: Docker and Kubernetes optimization strategies
- **Community Forum**: Share experiences and optimization techniques

---

**AshProfiler** transforms performance optimization from reactive debugging to proactive engineering. By embedding performance analysis directly into your development workflow, it ensures your Ash applications scale efficiently from day one.

*Join the growing community of developers building high-performance Ash applications with AshProfiler.*