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
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
# Bug Discovery & Feedback Loops

Automated bug discovery through fuzzing, chaos engineering, property-based testing, and browser discovery with complete feedback loop integration.

## Overview

Atom's bug discovery system automatically finds bugs through multiple discovery methods and closes the feedback loop with:

1. **Automated Discovery**: Fuzzing, chaos, property tests, browser exploration
2. **Unified Pipeline**: DiscoveryCoordinator orchestrates all methods
3. **Automated Filing**: BugFilingService creates GitHub Issues
4. **Regression Tests**: RegressionTestGenerator converts bugs to pytest tests
5. **Fix Verification**: BugFixVerifier re-runs tests and auto-closes issues
6. **ROI Tracking**: ROITracker demonstrates value of automation

## Quick Start

### Run Full Discovery

```python

from tests.bug_discovery.core import run_discovery

import os



result = run_discovery(

    github_token=os.getenv("GITHUB_TOKEN"),

    github_repository=os.getenv("GITHUB_REPOSITORY"),

    duration_seconds=3600  # 1 hour

)



print(f"Bugs found: {result['bugs_found']}")

print(f"Unique bugs: {result['unique_bugs']}")

print(f"ROI: {result['roi_data']['roi_ratio']:.1f}x")

```

### Generate Regression Tests

```python

from tests.bug_discovery.feedback_loops import RegressionTestGenerator

from tests.bug_discovery.models.bug_report import BugReport, DiscoveryMethod, Severity

from datetime import datetime



generator = RegressionTestGenerator()



# Generate test from bug report

bug = BugReport(

    discovery_method=DiscoveryMethod.FUZZING,

    test_name="test_api_fuzzing",

    error_message="SQL injection in agent_id",

    error_signature="abc123def4567890",

    severity=Severity.CRITICAL

)



test_path = generator.generate_test_from_bug(bug)

print(f"Generated: {test_path}")

```

### Verify Bug Fixes

```python

from tests.bug_discovery.feedback_loops import BugFixVerifier

import os



verifier = BugFixVerifier(

    github_token=os.getenv("GITHUB_TOKEN"),

    github_repository=os.getenv("GITHUB_REPOSITORY")

)



results = verifier.verify_fixes(label="fix", hours_ago=24)



for r in results:

    if r["test_passed"]:

        print(f"Issue #{r['issue_number']}: VERIFIED ✅")

    else:

        print(f"Issue #{r['issue_number']}: FAILED ❌")

```

### Track ROI

```python

from tests.bug_discovery.feedback_loops import ROITracker



tracker = ROITracker()



# Record discovery run

tracker.record_discovery_run(

    bugs_found=42,

    unique_bugs=35,

    filed_bugs=30,

    duration_seconds=3600,

    by_method={"fuzzing": 20, "chaos": 10, "property": 8, "browser": 4},

    by_severity={"critical": 2, "high": 10, "medium": 15, "low": 15}

)



# Generate ROI report

roi_report = tracker.generate_roi_report(weeks=4)

print(f"Hours Saved: {roi_report['hours_saved']:.0f}h")

print(f"Cost Saved: ${roi_report['cost_saved']:,.0f}")

print(f"ROI: {roi_report['roi_ratio']:.1f}x")

```

## Architecture

### Discovery Pipeline

```

DiscoveryCoordinator


1. Fuzzing Discovery (FuzzingOrchestrator)

    → BugReport objects


2. Chaos Discovery (ChaosCoordinator)

    → BugReport objects


3. Property Discovery (pytest)

    → BugReport objects


4. Browser Discovery (Playwright)

    → BugReport objects


5. Aggregation (ResultAggregator)

    → Normalized BugReport list


6. Deduplication (BugDeduplicator)

    → Unique BugReport list


7. Severity Classification (SeverityClassifier)

    → BugReport with severity


8. Bug Filing (BugFilingService)

    → GitHub Issues


9. Regression Tests (RegressionTestGenerator)

    → pytest test files


10. ROI Tracking (ROITracker)

    → Metrics database


11. Report Generation (DashboardGenerator)

    → HTML/JSON reports

```

### Feedback Loops

```

Discovered Bug


RegressionTestGenerator → test_regression_{method}_{bug_id}.py


Developer fixes bug, labels issue "fix"


BugFixVerifier detects label, re-runs test


Test passes 2x consecutively? → Close issue ✅

Test fails? → Add failure comment, keep open ❌

```

### ROI Calculation

```

Manual QA Cost:

  = bugs_found × manual_qa_hours_per_bug × manual_qa_hourly_rate

  = 50 × 2 × $75 = $7,500



Automation Cost:

  = duration_seconds / 3600 × developer_hourly_rate

  = 3600 / 3600 × $100 = $100



Cost Saved:

  = Manual QA Cost - Automation Cost

  = $7,500 - $100 = $7,400



Bugs Prevented:

  = bugs_found × 10% × bug_production_cost

  = 50 × 0.1 × $10,000 = $50,000



Total Savings:

  = Cost Saved + Bugs Prevented

  = $7,400 + $50,000 = $57,400



ROI Ratio:

  = Total Savings / Automation Cost

  = $57,400 / $100 = 574x

```

## Directory Structure

```

backend/tests/bug_discovery/

├── bug_filing_service.py          # GitHub Issues automation

├── feedback_loops/                # Feedback loop services

│   ├── __init__.py

│   ├── regression_test_generator.py

│   ├── bug_fix_verifier.py

│   ├── roi_tracker.py

│   ├── tests/

│   │   ├── test_regression_test_generator.py

│   │   ├── test_bug_fix_verifier.py

│   │   ├── test_roi_tracker.py

│   │   └── test_dashboard_enhancements.py

│   └── README.md                  # Feedback loops documentation

├── core/                          # Core orchestration

│   ├── __init__.py

│   ├── discovery_coordinator.py   # Main orchestrator

│   ├── result_aggregator.py

│   ├── bug_deduplicator.py

│   ├── severity_classifier.py

│   └── dashboard_generator.py     # Report generation

├── models/

│   ├── __init__.py

│   └── bug_report.py              # Unified bug model

├── storage/                       # Data storage

│   ├── reports/                   # Weekly HTML/JSON reports

│   ├── regression_tests/          # Generated regression tests

│   │   ├── archived/              # Verified fixes (archived)

│   │   └── .gitkeep

│   ├── metrics.db                 # ROI metrics database

│   └── bug_reports.db             # Bug database

├── fuzzing/                       # Fuzzing tests

├── chaos/                         # Chaos engineering tests

├── property_tests/                # Property-based tests

├── browser_discovery/             # Browser discovery tests

└── README.md                      # This file

```

## Discovery Methods

### Fuzzing (FUZZING)

Coverage-guided fuzzing for FastAPI endpoints using Atheris.

**What it finds:** SQL injection, XSS, CSRF, buffer overflows, parsing errors

**Example:**
```python

from tests.fuzzing.campaigns.fuzzing_orchestrator import FuzzingOrchestrator



orchestrator = FuzzingOrchestrator(github_token, github_repository)

result = orchestrator.run_campaign_with_bug_filing(

    target_endpoint="/api/v1/agents",

    test_file="tests/fuzzing/test_agent_api_fuzzing.py",

    duration_seconds=900

)

```

### Chaos Engineering (CHAOS)

Controlled failure injection testing resilience.

**What it finds:** Connection timeouts, memory exhaustion, cascading failures

**Example:**
```python

from tests.chaos.core.chaos_coordinator import ChaosCoordinator



coordinator = ChaosCoordinator(db_session=test_db, bug_filing_service=filing_service)

result = coordinator.run_experiment(

    experiment_name="network_latency_3g",

    failure_injection=LatencyInjection,

    verify_graceful_degradation=lambda: True,

    blast_radius_checks=[assert_blast_radius]

)

```

### Property-Based Testing (PROPERTY)

Hypothesis-based invariant testing.

**What it finds:** State machine violations, contract violations, edge cases

**Example:**
```bash

pytest tests/property_tests/ -v

```

### Browser Discovery (BROWSER)

Headless browser automation for UI bug detection.

**What it finds:** Console errors, accessibility violations, broken links

**Example:**
```python

from tests.browser_discovery.conftest import authenticated_page



page.goto("http://localhost:3000/dashboard")

errors = page.evaluate("() => window.__consoleErrors || []")

assert len(errors) == 0

```

## Configuration

### Environment Variables

```bash

# GitHub Integration

GITHUB_TOKEN=ghp_xxx                    # GitHub Personal Access Token

GITHUB_REPOSITORY=owner/repo             # Repository for issue filing



# Discovery Configuration

DISCOVERY_DURATION_SECONDS=3600          # Duration per discovery method

ENABLE_REGRESSION_TESTS=true            # Generate regression tests

ENABLE_ROI_TRACKING=true                # Track ROI metrics



# ROI Cost Assumptions (optional, uses defaults if not set)

MANUAL_QA_HOURLY_RATE=75                # Cost per hour for manual QA

DEVELOPER_HOURLY_RATE=100               # Cost per hour for developer

BUG_PRODUCTION_COST=10000               # Average cost of production bug

MANUAL_QA_HOURS_PER_BUG=2               # Hours to manually find a bug

```

### Pytest Configuration

```ini

# pytest.ini



[pytest]

markers =

    regression: Marks tests as regression tests

    fuzzing: Marks tests as fuzzing tests

    chaos: Marks tests as chaos engineering tests

    property: Marks tests as property-based tests

    browser: Marks tests as browser discovery tests

    slow: Marks tests as slow (run in weekly pipeline only)

```

## CI/CD Integration

### Weekly Bug Discovery Pipeline

Runs every Sunday at 2 AM UTC:

```yaml

# .github/workflows/bug-discovery-weekly.yml

on:

  schedule:

    - cron: '0 2 * * 0'  # Sunday 2 AM UTC



jobs:

  bug-discovery:

    steps:

      - uses: actions/checkout@v4

      - run: |

          python -c "

          from tests.bug_discovery.core import run_discovery

          run_discovery(

              github_token=os.getenv('GITHUB_TOKEN'),

              github_repository=os.getenv('GITHUB_REPOSITORY'),

              duration_seconds=3600

          )

          "

```

### Bug Fix Verification Pipeline

Runs every 6 hours to verify fixes:

```yaml

# .github/workflows/bug-fix-verification.yml

on:

  schedule:

    - cron: '0 */6 * * *'  # Every 6 hours



jobs:

  verify-fixes:

    steps:

      - run: |

          python -c "

          from tests.bug_discovery.feedback_loops import BugFixVerifier

          verifier = BugFixVerifier(...)

          verifier.verify_fixes()

          "

```

## Best Practices

### Writing Regression Tests

Generated regression tests should be reviewed and enhanced:

```python

# Auto-generated (minimal)

def test_regression_fuzzing_abc123de(api_client):

    # TODO: Implement test logic

    assert True  # Placeholder



# Enhanced (manual review)

def test_regression_fuzzing_abc123de(api_client):

    """Regression test for SQL injection in agent_id parameter."""

    # Test with malicious input

    response = api_client.post("/api/v1/agents", json={

        "agent_id": "1 OR 1=1--"

    })



    # Should reject malicious input

    assert response.status_code in [400, 422]



    # Test with valid input

    response = api_client.post("/api/v1/agents", json={

        "agent_id": "valid-uuid"

    })



    # Should accept valid input

    assert response.status_code == 200

```

### Archival Strategy

Regression tests are archived when:

1. Bug fix is verified (test passes 2x consecutively)
2. GitHub issue is closed
3. Test moved to `regression_tests/archived/`

Restoration:

If a bug recurs (same error_signature detected):

1. Test moved back from `archived/`

2. Re-run to confirm bug exists

3. Re-open GitHub issue if confirmed



## Metrics & Effectiveness



### Key Metrics



| Metric | Description | Target |

|--------|-------------|--------|

| Bugs found per hour | Discovery throughput | > 10/hour |

| Unique bug rate | Deduplication effectiveness | > 70% |

| False positive rate | Bugs not confirmed | < 5% |

| Fix verification rate | Bugs with verified fixes | > 80% |

| ROI ratio | Cost savings vs automation cost | > 10x |



### Weekly Report Sections



1. **Bug Discovery Summary**: Bugs found, unique, filed

2. **ROI Metrics**: Hours saved, cost saved, bugs prevented, ROI ratio

3. **Fix Verification**: Fixed, verified, pending counts

4. **By Method**: Breakdown by discovery method

5. **By Severity**: Breakdown by severity

6. **Top Bugs**: Highest priority bugs



## Troubleshooting



### Regression Test Generation Fails



```bash

# Check templates exist

ls backend/tests/bug_discovery/templates/*.j2



# Verify BugReport has required fields

python -c "from tests.bug_discovery.models import BugReport; print(BugReport.__fields__)"

```



### Bug Fix Verification Not Running



```bash

# Check GitHub token has repo scope

# Check workflow is enabled in GitHub Actions

# Verify "fix" label exists in repository

```



### ROI Metrics Not Appearing



```bash

# Check metrics.db exists

ls backend/tests/bug_discovery/storage/metrics.db



# Verify ROI tracking enabled

python -c "from tests.bug_discovery.core import DiscoveryCoordinator; c = DiscoveryCoordinator(...); print(c.enable_roi_tracking)"

```



## Further Reading



- [Bug Discovery Infrastructure](./docs/BUG_DISCOVERY_INFRASTRUCTURE.md)

- [Feedback Loops Documentation](./feedback_loops/README.md)

- [Property-Based Testing](./property_tests/README.md)

- [Chaos Engineering](./chaos/README.md)

- [Fuzzing Guide](./fuzzing/README.md)



## Contributing



When adding new discovery methods:



1. Create BugReport objects with required fields

2. Add method to DiscoveryMethod enum

3. Implement aggregation in ResultAggregator

4. Update DashboardGenerator templates

5. Add regression test template