code_healer 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +70 -0
  3. data/GEM_SUMMARY.md +307 -0
  4. data/README.md +281 -0
  5. data/code_healer.gemspec +77 -0
  6. data/config/code_healer.yml.example +104 -0
  7. data/docs/INSTALLATION.md +439 -0
  8. data/examples/basic_usage.rb +160 -0
  9. data/exe/code_healer-setup +7 -0
  10. data/lib/code_healer/application_job.rb +7 -0
  11. data/lib/code_healer/business_context_analyzer.rb +464 -0
  12. data/lib/code_healer/business_context_loader.rb +273 -0
  13. data/lib/code_healer/business_context_manager.rb +297 -0
  14. data/lib/code_healer/business_logic_generator.rb +94 -0
  15. data/lib/code_healer/business_rule_applier.rb +54 -0
  16. data/lib/code_healer/claude_code_evolution_handler.rb +224 -0
  17. data/lib/code_healer/claude_error_monitor.rb +48 -0
  18. data/lib/code_healer/config_manager.rb +275 -0
  19. data/lib/code_healer/context_aware_prompt_builder.rb +153 -0
  20. data/lib/code_healer/core.rb +513 -0
  21. data/lib/code_healer/error_handler.rb +141 -0
  22. data/lib/code_healer/evolution_job.rb +99 -0
  23. data/lib/code_healer/global_handler.rb +130 -0
  24. data/lib/code_healer/healing_job.rb +167 -0
  25. data/lib/code_healer/mcp.rb +108 -0
  26. data/lib/code_healer/mcp_prompts.rb +111 -0
  27. data/lib/code_healer/mcp_server.rb +389 -0
  28. data/lib/code_healer/mcp_tools.rb +2364 -0
  29. data/lib/code_healer/pull_request_creator.rb +143 -0
  30. data/lib/code_healer/setup.rb +390 -0
  31. data/lib/code_healer/simple_evolution.rb +737 -0
  32. data/lib/code_healer/simple_global_handler.rb +122 -0
  33. data/lib/code_healer/simple_healer.rb +515 -0
  34. data/lib/code_healer/terminal_integration.rb +87 -0
  35. data/lib/code_healer/usage_analyzer.rb +92 -0
  36. data/lib/code_healer/version.rb +5 -0
  37. data/lib/code_healer.rb +67 -0
  38. metadata +411 -0
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/code_healer/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "code_healer"
7
+ spec.version = CodeHealer::VERSION
8
+ spec.authors = ["Deepan Kumar"]
9
+ spec.email = ["deepan.ppgit@gmail.com"]
10
+
11
+ spec.summary = "AI-powered code healing and self-repair system for Ruby applications"
12
+ spec.description = <<~DESC
13
+ CodeHealer is a revolutionary gem that enables your Ruby applications to
14
+ automatically detect, analyze, and fix errors using AI. It integrates
15
+ with OpenAI API, Claude Code terminal, and provides intelligent error handling,
16
+ business context awareness, and automated Git operations.
17
+
18
+ Features:
19
+ - ๐Ÿค– AI-powered error analysis and code generation
20
+ - ๐ŸŽฏ Business context-aware fixes
21
+ - ๐Ÿ”„ Multiple healing strategies (API, Claude Code, Hybrid)
22
+ - ๐Ÿ“ Automated Git operations and PR creation
23
+ - ๐Ÿ“‹ Business requirements integration from markdown
24
+ - โšก Background job processing with Sidekiq
25
+ - ๐ŸŽจ Configurable via YAML files
26
+ DESC
27
+
28
+ spec.homepage = "https://github.com/deepan-g2/code-healer"
29
+ spec.license = "MIT"
30
+ spec.required_ruby_version = ">= 2.7.0"
31
+
32
+ spec.metadata["homepage_uri"] = "https://github.com/deepan-g2/code-healer"
33
+ spec.metadata["source_code_uri"] = "https://github.com/deepan-g2/code-healer"
34
+ spec.metadata["changelog_uri"] = "https://github.com/deepan-g2/code-healer/blob/main/CHANGELOG.md"
35
+ spec.metadata["bug_tracker_uri"] = "https://github.com/deepan-g2/code-healer/issues"
36
+ spec.metadata["documentation_uri"] = "https://github.com/deepan-g2/code-healer/blob/main/README.md"
37
+
38
+ # Specify which files should be added to the gem when it is released
39
+ spec.files = Dir[
40
+ "lib/**/*",
41
+ "bin/**/*",
42
+ "config/**/*",
43
+ "docs/**/*",
44
+ "examples/**/*",
45
+ "*.md",
46
+ "code_healer.gemspec"
47
+ ]
48
+
49
+ spec.require_paths = ["lib"]
50
+ spec.bindir = "exe"
51
+ spec.executables = ["code_healer-setup"]
52
+
53
+ # Runtime dependencies
54
+ spec.add_runtime_dependency 'rails', '>= 6.0.0'
55
+ spec.add_runtime_dependency 'sidekiq', '>= 6.0.0'
56
+ spec.add_runtime_dependency 'redis', '~> 4.0', '>= 4.0.0'
57
+ spec.add_runtime_dependency 'octokit', '~> 4.0', '>= 4.0.0'
58
+ spec.add_runtime_dependency 'git', '~> 1.0', '>= 1.0.0'
59
+ spec.add_runtime_dependency 'openai', '~> 0.16.0', '>= 0.16.0'
60
+ spec.add_runtime_dependency 'activesupport', '>= 6.0.0'
61
+ spec.add_runtime_dependency 'actionpack', '>= 6.0.0'
62
+ spec.add_runtime_dependency 'activemodel', '>= 6.0.0'
63
+
64
+ # Development dependencies
65
+ spec.add_development_dependency "bundler", ">= 2.0.0"
66
+ spec.add_development_dependency "rake", ">= 13.0.0"
67
+ spec.add_development_dependency "rspec", ">= 3.0.0"
68
+ spec.add_development_dependency "rspec-rails", ">= 5.0.0"
69
+ spec.add_development_dependency "factory_bot_rails", ">= 6.0.0"
70
+ spec.add_development_dependency "faker", ">= 2.0.0"
71
+ spec.add_development_dependency "webmock", ">= 3.0.0"
72
+ spec.add_development_dependency "vcr", ">= 6.0.0"
73
+ spec.add_development_dependency "rubocop", ">= 1.0.0"
74
+ spec.add_development_dependency "rubocop-rails", ">= 2.0.0"
75
+ spec.add_development_dependency "yard", ">= 0.9.0"
76
+ spec.add_development_dependency "redcarpet", ">= 3.0.0"
77
+ end
@@ -0,0 +1,104 @@
1
+ ---
2
+ # CodeHealer Configuration Example
3
+ # Copy this file to config/code_healer.yml and customize for your project
4
+
5
+ enabled: true
6
+
7
+ # Classes that are allowed to evolve
8
+ allowed_classes:
9
+ - User
10
+ - Order
11
+ - PaymentProcessor
12
+ - InventoryManager
13
+ - Api::UserController
14
+ - Api::OrderController
15
+
16
+ # Classes that should never evolve
17
+ excluded_classes:
18
+ - ApplicationController
19
+ - ApplicationRecord
20
+ - ApplicationJob
21
+ - ApplicationMailer
22
+
23
+ # Error types that should trigger evolution
24
+ allowed_error_types:
25
+ - ArgumentError
26
+ - NameError
27
+ - NoMethodError
28
+ - TypeError
29
+ - ValidationError
30
+
31
+ # Evolution Strategy Configuration
32
+ evolution_strategy:
33
+ method: "api" # Options: "api", "claude_code_terminal", "hybrid"
34
+ fallback_to_api: true # If Claude Code fails, fall back to API
35
+
36
+ # Claude Code Terminal Configuration (for local AI agent)
37
+ claude_code:
38
+ enabled: true
39
+ timeout: 300 # 5 minutes timeout
40
+ max_file_changes: 10
41
+ include_tests: true
42
+ command_template: "claude --print '{prompt}' --output-format text --permission-mode acceptEdits --allowedTools Edit"
43
+ business_context_sources:
44
+ - "config/business_rules.yml"
45
+ - "docs/business_logic.md"
46
+ - "spec/business_context_specs.rb"
47
+
48
+ # Business Context Configuration
49
+ business_context:
50
+ enabled: true
51
+
52
+ # Define business rules for specific classes
53
+ User:
54
+ domain: "User Management"
55
+ key_rules:
56
+ - "Email must be unique and valid"
57
+ - "Password must meet security requirements"
58
+ - "User data must be validated"
59
+ validation_patterns:
60
+ - "Email format validation"
61
+ - "Password strength requirements"
62
+ - "Data integrity checks"
63
+
64
+ Order:
65
+ domain: "E-commerce Order Processing"
66
+ key_rules:
67
+ - "Orders must have valid customer information"
68
+ - "Payment validation is required"
69
+ - "Inventory must be checked before processing"
70
+ validation_patterns:
71
+ - "Input validation for all parameters"
72
+ - "Business rule enforcement"
73
+ - "Error handling with meaningful messages"
74
+
75
+ # API Configuration (for OpenAI integration)
76
+ api:
77
+ provider: "openai"
78
+ model: "gpt-4"
79
+ max_tokens: 2000
80
+ temperature: 0.1
81
+
82
+ # Git Configuration
83
+ git:
84
+ auto_commit: true
85
+ auto_push: true
86
+ branch_prefix: evolve
87
+ commit_message_template: 'Fix {class_name}##{method_name}: {error_type}'
88
+ pr_target_branch: main # or your preferred target branch
89
+
90
+ # Pull Request Configuration
91
+ pull_request:
92
+ enabled: true
93
+ auto_create: true
94
+ title_template: 'Fix {class_name}##{method_name}: Handle {error_type}'
95
+ labels:
96
+ - "auto-fix"
97
+ - "self-evolving"
98
+ - "bug-fix"
99
+
100
+ # Sidekiq Configuration (for background processing)
101
+ sidekiq:
102
+ queue: "evolution"
103
+ retry: 3
104
+ backtrace: true
@@ -0,0 +1,439 @@
1
+ # Installation Guide ๐Ÿš€
2
+
3
+ This guide will walk you through installing and setting up CodeHealer in your Ruby on Rails application.
4
+
5
+ ## ๐Ÿ“‹ Prerequisites
6
+
7
+ Before installing CodeHealer, ensure you have:
8
+
9
+ - Ruby 2.7.0 or higher
10
+ - Rails 6.0.0 or higher
11
+ - Git repository initialized
12
+ - GitHub account (for pull request creation)
13
+ - OpenAI API key (for API-based evolution)
14
+ - Claude Code terminal (for local evolution)
15
+
16
+ ## ๐Ÿ”ง Step-by-Step Installation
17
+
18
+ ### 1. Add to Gemfile
19
+
20
+ Add the gem to your `Gemfile`:
21
+
22
+ ```ruby
23
+ # Gemfile
24
+ source 'https://rubygems.org'
25
+
26
+ # ... other gems ...
27
+
28
+ # CodeHealer - AI-powered code healing and self-repair system
29
+ gem 'code_healer'
30
+
31
+ # Required dependencies (if not already present)
32
+ gem 'sidekiq', '>= 6.0.0'
33
+ gem 'redis', '>= 4.0.0'
34
+ ```
35
+
36
+ ### 2. Install Dependencies
37
+
38
+ Run bundle install:
39
+
40
+ ```bash
41
+ bundle install
42
+ ```
43
+
44
+ ### 3. Copy Configuration
45
+
46
+ Copy the example configuration file:
47
+
48
+ ```bash
49
+ cp config/code_healer.yml.example config/code_healer.yml
50
+ ```
51
+
52
+ ### 4. Configure CodeHealer
53
+
54
+ Edit `config/code_healer.yml` with your settings:
55
+
56
+ ```yaml
57
+ ---
58
+ enabled: true
59
+
60
+ # Classes that are allowed to evolve
61
+ allowed_classes:
62
+ - User
63
+ - Order
64
+ - PaymentProcessor
65
+
66
+ # Evolution strategy
67
+ evolution_strategy:
68
+ method: "api" # or "claude_code_terminal" or "hybrid"
69
+
70
+ # API configuration
71
+ api:
72
+ provider: "openai"
73
+ model: "gpt-4"
74
+ api_key: <%= ENV['OPENAI_API_KEY'] %>
75
+
76
+ # Business context
77
+ business_context:
78
+ enabled: true
79
+ User:
80
+ domain: "User Management"
81
+ key_rules:
82
+ - "Email must be unique and valid"
83
+ ```
84
+
85
+ ### 5. Set Environment Variables
86
+
87
+ Create or update your `.env` file:
88
+
89
+ ```bash
90
+ # .env
91
+ OPENAI_API_KEY=your_openai_api_key_here
92
+ GITHUB_TOKEN=your_github_token_here
93
+ ```
94
+
95
+ **Important**: Never commit API keys to version control!
96
+
97
+ ### 6. Setup Sidekiq (Recommended)
98
+
99
+ Create `config/sidekiq.yml`:
100
+
101
+ ```yaml
102
+ :concurrency: 5
103
+ :queues:
104
+ - [evolution, 2]
105
+ - [default, 1]
106
+ ```
107
+
108
+ Add to `config/routes.rb`:
109
+
110
+ ```ruby
111
+ # config/routes.rb
112
+ require 'sidekiq/web'
113
+
114
+ Rails.application.routes.draw do
115
+ # ... other routes ...
116
+
117
+ # Sidekiq Web UI (optional, for monitoring)
118
+ mount Sidekiq::Web => '/sidekiq'
119
+ end
120
+ ```
121
+
122
+ ### 7. Setup Redis
123
+
124
+ Ensure Redis is running:
125
+
126
+ ```bash
127
+ # macOS with Homebrew
128
+ brew services start redis
129
+
130
+ # Ubuntu/Debian
131
+ sudo systemctl start redis
132
+
133
+ # Or run manually
134
+ redis-server
135
+ ```
136
+
137
+ ### 8. Business Requirements (Optional)
138
+
139
+ Create a `business_requirements/` directory in your project root:
140
+
141
+ ```bash
142
+ mkdir business_requirements
143
+ ```
144
+
145
+ Add markdown files with your business rules:
146
+
147
+ ```markdown
148
+ # business_requirements/user_management.md
149
+ # User Management Business Rules
150
+
151
+ ## Authentication
152
+ - Users must have valid email addresses
153
+ - Passwords must be at least 8 characters
154
+ - Failed login attempts should be logged
155
+
156
+ ## Data Validation
157
+ - All user input must be sanitized
158
+ - Email uniqueness is enforced at database level
159
+ ```
160
+
161
+ ## ๐Ÿš€ Quick Start Example
162
+
163
+ ### 1. Create a Test Model
164
+
165
+ ```ruby
166
+ # app/models/user.rb
167
+ class User < ApplicationRecord
168
+ def calculate_discount(amount, percentage)
169
+ # This will trigger evolution if an error occurs
170
+ amount * (percentage / 100.0)
171
+ end
172
+ end
173
+ ```
174
+
175
+ ### 2. Test Evolution
176
+
177
+ Start your Rails server and trigger an error:
178
+
179
+ ```bash
180
+ rails server
181
+ ```
182
+
183
+ In another terminal, trigger an error:
184
+
185
+ ```bash
186
+ curl -X POST http://localhost:3000/api/users/calculate_discount \
187
+ -H "Content-Type: application/json" \
188
+ -d '{"amount": 100, "percentage": nil}'
189
+ ```
190
+
191
+ ### 3. Watch Evolution in Action
192
+
193
+ Check the logs to see CodeHealer in action:
194
+
195
+ ```bash
196
+ tail -f log/development.log
197
+ ```
198
+
199
+ ## ๐Ÿ” Verification
200
+
201
+ ### Check Installation
202
+
203
+ Verify CodeHealer is properly installed:
204
+
205
+ ```ruby
206
+ # In Rails console
207
+ rails console
208
+
209
+ # Check if CodeHealer is loaded
210
+ CodeHealer::VERSION
211
+ # Should return the version number
212
+
213
+ # Check configuration
214
+ CodeHealer::ConfigManager.config
215
+ # Should return your configuration hash
216
+ ```
217
+
218
+ ### Check Sidekiq
219
+
220
+ Verify Sidekiq is working:
221
+
222
+ ```bash
223
+ # Start Sidekiq
224
+ bundle exec sidekiq
225
+
226
+ # Check Sidekiq Web UI
227
+ open http://localhost:3000/sidekiq
228
+ ```
229
+
230
+ ### Check Business Context
231
+
232
+ Verify business context is loading:
233
+
234
+ ```ruby
235
+ # In Rails console
236
+ CodeHealer::BusinessContextManager.get_context_for_error(
237
+ ArgumentError.new("test"),
238
+ "User",
239
+ "calculate_discount"
240
+ )
241
+ ```
242
+
243
+ ## ๐Ÿ› ๏ธ Configuration Options
244
+
245
+ ### Core Settings
246
+
247
+ | Setting | Description | Default | Required |
248
+ |---------|-------------|---------|----------|
249
+ | `enabled` | Enable/disable CodeHealer | `true` | Yes |
250
+ | `allowed_classes` | Classes that can evolve | `[]` | Yes |
251
+ | `excluded_classes` | Classes that should never evolve | `[]` | No |
252
+ | `allowed_error_types` | Error types that trigger evolution | `[]` | No |
253
+
254
+ ### Evolution Strategy
255
+
256
+ | Strategy | Description | Use Case |
257
+ |----------|-------------|----------|
258
+ | `api` | Use OpenAI API for fixes | Production, cloud-based |
259
+ | `claude_code_terminal` | Use local Claude Code agent | Development, offline |
260
+ | `hybrid` | Try Claude Code first, fallback to API | Best of both worlds |
261
+
262
+ ### Business Context
263
+
264
+ ```yaml
265
+ business_context:
266
+ enabled: true
267
+
268
+ User:
269
+ domain: "User Management"
270
+ key_rules:
271
+ - "Email must be unique and valid"
272
+ validation_patterns:
273
+ - "Input validation for all parameters"
274
+ ```
275
+
276
+ ### Git Operations
277
+
278
+ ```yaml
279
+ git:
280
+ auto_commit: true
281
+ auto_push: true
282
+ branch_prefix: evolve
283
+ pr_target_branch: main
284
+
285
+ pull_request:
286
+ enabled: true
287
+ auto_create: true
288
+ labels: ["auto-fix", "self-evolving"]
289
+ ```
290
+
291
+ ## ๐Ÿ”’ Security Configuration
292
+
293
+ ### API Keys
294
+
295
+ Store sensitive data in environment variables:
296
+
297
+ ```yaml
298
+ # config/self_evolution.yml
299
+ api:
300
+ provider: "openai"
301
+ api_key: <%= ENV['OPENAI_API_KEY'] %>
302
+ model: "gpt-4"
303
+ ```
304
+
305
+ ### Class Restrictions
306
+
307
+ Carefully configure which classes can evolve:
308
+
309
+ ```yaml
310
+ allowed_classes:
311
+ - User
312
+ - Order
313
+ - PaymentProcessor
314
+
315
+ excluded_classes:
316
+ - ApplicationController
317
+ - ApplicationRecord
318
+ - ApplicationJob
319
+ - ApplicationMailer
320
+ ```
321
+
322
+ ### Business Rule Validation
323
+
324
+ Enable business rule validation:
325
+
326
+ ```yaml
327
+ business_context:
328
+ enabled: true
329
+ validate_rules: true
330
+ strict_mode: true
331
+ ```
332
+
333
+ ## ๐Ÿงช Testing Configuration
334
+
335
+ ### Test Environment
336
+
337
+ Disable evolution in test environment:
338
+
339
+ ```yaml
340
+ # config/environments/test.rb
341
+ Rails.application.configure do
342
+ # ... other config ...
343
+
344
+ # Disable CodeHealer in tests
345
+ config.self_evolving = {
346
+ enabled: false,
347
+ mock_ai_responses: true,
348
+ dry_run: true
349
+ }
350
+ end
351
+ ```
352
+
353
+ ### Mock Responses
354
+
355
+ Use mock responses for testing:
356
+
357
+ ```yaml
358
+ # config/self_evolution.yml
359
+ test:
360
+ enabled: false
361
+ mock_ai_responses: true
362
+ mock_response: "def calculate_discount(amount, percentage)\n return 0 if amount.nil? || percentage.nil?\n amount * (percentage / 100.0)\nend"
363
+ ```
364
+
365
+ ## ๐Ÿšจ Troubleshooting
366
+
367
+ ### Common Installation Issues
368
+
369
+ 1. **Gem not found**
370
+ ```bash
371
+ bundle install
372
+ bundle exec rails console
373
+ ```
374
+
375
+ 2. **Configuration not loading**
376
+ - Check file path: `config/self_evolution.yml`
377
+ - Verify YAML syntax
378
+ - Check file permissions
379
+
380
+ 3. **Sidekiq not working**
381
+ ```bash
382
+ # Check Redis
383
+ redis-cli ping
384
+
385
+ # Check Sidekiq
386
+ bundle exec sidekiq -V
387
+ ```
388
+
389
+ 4. **Business context not loading**
390
+ - Verify `business_requirements/` directory exists
391
+ - Check markdown file syntax
392
+ - Verify file permissions
393
+
394
+ ### Debug Mode
395
+
396
+ Enable debug logging:
397
+
398
+ ```yaml
399
+ # config/self_evolution.yml
400
+ logging:
401
+ level: debug
402
+ show_thinking_process: true
403
+ verbose: true
404
+ ```
405
+
406
+ ### Log Files
407
+
408
+ Check relevant log files:
409
+
410
+ ```bash
411
+ # Rails logs
412
+ tail -f log/development.log
413
+
414
+ # Sidekiq logs
415
+ tail -f log/sidekiq.log
416
+
417
+ # Redis logs
418
+ tail -f /var/log/redis/redis-server.log
419
+ ```
420
+
421
+ ## ๐Ÿ“š Next Steps
422
+
423
+ After successful installation:
424
+
425
+ 1. **Read the [README](README.md)** for comprehensive usage information
426
+ 2. **Check [Configuration](CONFIGURATION.md)** for advanced options
427
+ 3. **Review [Examples](EXAMPLES.md)** for practical use cases
428
+ 4. **Join [Discussions](https://github.com/deepan-g2/self-evolving/discussions)** for community support
429
+
430
+ ## ๐Ÿ†˜ Need Help?
431
+
432
+ - ๐Ÿ“ง **Email**: deepan@example.com
433
+ - ๐Ÿ› **Issues**: [GitHub Issues](https://github.com/deepan-g2/self-evolving/issues)
434
+ - ๐Ÿ’ฌ **Discussions**: [GitHub Discussions](https://github.com/deepan-g2/self-evolving/discussions)
435
+ - ๐Ÿ“– **Wiki**: [GitHub Wiki](https://github.com/deepan-g2/self-evolving/wiki)
436
+
437
+ ---
438
+
439
+ **Happy Evolving! ๐Ÿš€**