File size: 25,617 Bytes
81e3673
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
#!/usr/bin/env python3
"""
Coverage Trend Tracking System for Atom v5.0

This script tracks coverage changes over time, establishing a baseline and
monitoring progress toward the 80% target. It records snapshots, calculates
deltas, maintains historical data, and generates trend visualizations.

Usage:
    # Record current coverage with commit hash
    python coverage_trend_tracker.py --commit <hash> --chart

    # Record from specific coverage file
    python coverage_trend_tracker.py --coverage-file path/to/coverage.json --commit <hash>

    # Check for regressions (CI usage)
    python coverage_trend_tracker.py --regression-check

    # Compare two commits
    python coverage_trend_tracker.py --compare-commits <hash1> <hash2>

    # Forecast when 80% target will be reached
    python coverage_trend_tracker.py --forecast 80

    # Record coverage for CI (generates PR comment payload)
    python coverage_trend_tracker.py --ci-record

Features:
    - Per-commit coverage tracking with automatic git hash detection
    - Baseline establishment for v5.0 expansion
    - Delta calculation (absolute and relative changes)
    - Historical trend data (last 30 entries)
    - ASCII visualization with 80% target marker
    - Regression detection (alerts on >1% decrease)
    - Timeline forecasting for 80% target
    - CI integration hooks for PR comments
"""

import argparse
import json
import subprocess
import sys
from datetime import datetime, timezone
from pathlib import Path
from typing import Dict, Any, List, Optional, Tuple


# Constants
DEFAULT_COVERAGE_FILE = Path("tests/coverage_reports/metrics/coverage.json")
DEFAULT_TREND_FILE = Path("tests/coverage_reports/metrics/coverage_trend_v5.0.json")
DEFAULT_TRENDS_DIR = Path("tests/coverage_reports/trends")
TARGET_COVERAGE = 80.0
REGRESSION_THRESHOLD = 1.0  # Alert if coverage decreases by >1 percentage point


def get_git_commit_hash() -> Optional[str]:
    """
    Get current git commit hash.

    Returns:
        Commit hash as string, or None if not in git repo
    """
    try:
        result = subprocess.run(
            ["git", "rev-parse", "HEAD"],
            capture_output=True,
            text=True,
            check=True
        )
        return result.stdout.strip()
    except (subprocess.CalledProcessError, FileNotFoundError):
        return None


def get_git_commit_message(commit_hash: str) -> Optional[str]:
    """
    Get git commit message for a given hash.

    Args:
        commit_hash: Git commit hash

    Returns:
        Commit message subject line, or None if not available
    """
    try:
        result = subprocess.run(
            ["git", "log", "-1", "--format=%s", commit_hash],
            capture_output=True,
            text=True,
            check=True
        )
        return result.stdout.strip()
    except (subprocess.CalledProcessError, FileNotFoundError):
        return None


def record_snapshot(coverage_data: Dict[str, Any], commit_hash: Optional[str] = None) -> Dict[str, Any]:
    """
    Extract coverage snapshot from coverage data.

    Args:
        coverage_data: Loaded coverage.json data
        commit_hash: Git commit hash (auto-detected if None)

    Returns:
        Snapshot dict with timestamp, commit, coverage metrics
    """
    # Extract overall metrics
    totals = coverage_data.get("totals", {})
    overall_coverage = totals.get("percent_covered", 0.0)
    covered_lines = totals.get("covered_lines", 0)
    total_lines = totals.get("num_statements", 0)
    covered_branches = totals.get("covered_branches", 0)
    total_branches = totals.get("num_branches", 0)

    # Extract module breakdown from files
    files = coverage_data.get("files", {})
    module_breakdown = {}

    for file_path, file_data in files.items():
        # Determine module from file path
        if file_path.startswith("core/"):
            module = "core"
        elif file_path.startswith("api/"):
            module = "api"
        elif file_path.startswith("tools/"):
            module = "tools"
        elif file_path.startswith("skills/"):
            module = "skills"
        else:
            module = "other"

        # Aggregate module coverage
        if module not in module_breakdown:
            module_breakdown[module] = {"covered": 0, "total": 0}

        summary = file_data.get("summary", {})
        module_breakdown[module]["covered"] += summary.get("covered_lines", 0)
        module_breakdown[module]["total"] += summary.get("num_statements", 0)

    # Calculate module percentages
    module_percentages = {}
    for module, data in module_breakdown.items():
        if data["total"] > 0:
            module_percentages[module] = (data["covered"] / data["total"]) * 100
        else:
            module_percentages[module] = 0.0

    # Get commit hash
    if commit_hash is None:
        commit_hash = get_git_commit_hash()

    # Get commit message
    commit_message = None
    if commit_hash:
        commit_message = get_git_commit_message(commit_hash)

    # Build snapshot
    snapshot = {
        "timestamp": datetime.now(timezone.utc).isoformat().replace("+00:00", "Z"),
        "commit": commit_hash,
        "commit_message": commit_message,
        "overall_coverage": round(overall_coverage, 2),
        "covered_lines": covered_lines,
        "total_lines": total_lines,
        "branch_coverage": round((covered_branches / total_branches * 100) if total_branches > 0 else 0.0, 2),
        "covered_branches": covered_branches,
        "total_branches": total_branches,
        "module_breakdown": {
            module: round(pct, 2)
            for module, pct in module_percentages.items()
        }
    }

    return snapshot


def get_trend_history(trend_file: Path = DEFAULT_TREND_FILE) -> Dict[str, Any]:
    """
    Load trend history from file, creating new structure if needed.

    Args:
        trend_file: Path to trend JSON file

    Returns:
        Trend data dict with baseline, history, current, metadata
    """
    # Default structure
    trend_data = {
        "baseline": None,  # Will be set on first snapshot
        "history": [],
        "current": None,
        "metadata": {
            "version": "5.0",
            "target_coverage": TARGET_COVERAGE,
            "max_history_entries": 30,
            "created_at": datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
        }
    }

    # Load existing data if available
    if trend_file.exists():
        try:
            with open(trend_file, 'r') as f:
                loaded_data = json.load(f)
                # Merge with default structure
                trend_data.update(loaded_data)
        except (json.JSONDecodeError, IOError) as e:
            print(f"Warning: Could not load trend file: {e}", file=sys.stderr)

    return trend_data


def calculate_delta(current: float, previous: float) -> Dict[str, Any]:
    """
    Calculate delta between two coverage values.

    Args:
        current: Current coverage percentage
        previous: Previous coverage percentage

    Returns:
        Dict with absolute_change, relative_change, direction
    """
    absolute_change = current - previous
    relative_change = (absolute_change / previous * 100) if previous > 0 else 0.0

    if absolute_change > 0:
        direction = "increase"
    elif absolute_change < 0:
        direction = "decrease"
    else:
        direction = "no_change"

    return {
        "absolute_change": round(absolute_change, 2),
        "relative_change": round(relative_change, 2),
        "direction": direction
    }


def update_trend_data(snapshot: Dict[str, Any], trend_data: Dict[str, Any]) -> Dict[str, Any]:
    """
    Update trend data with new snapshot.

    Args:
        snapshot: Coverage snapshot to add
        trend_data: Existing trend data

    Returns:
        Updated trend data
    """
    # Calculate delta from previous snapshot
    if trend_data["history"]:
        previous_coverage = trend_data["history"][-1]["overall_coverage"]
        snapshot["delta"] = calculate_delta(snapshot["overall_coverage"], previous_coverage)
    else:
        snapshot["delta"] = {
            "absolute_change": 0.0,
            "relative_change": 0.0,
            "direction": "baseline"
        }

    # Append to history
    trend_data["history"].append(snapshot)

    # Keep only last 30 entries
    if len(trend_data["history"]) > trend_data["metadata"]["max_history_entries"]:
        trend_data["history"] = trend_data["history"][-trend_data["metadata"]["max_history_entries"]:]

    # Update current
    trend_data["current"] = snapshot

    # Set baseline if not set
    if trend_data["baseline"] is None:
        trend_data["baseline"] = snapshot

    # Update metadata
    trend_data["metadata"]["last_updated"] = datetime.now(timezone.utc).isoformat().replace("+00:00", "Z")
    trend_data["metadata"]["total_snapshots"] = len(trend_data["history"])

    return trend_data


def write_trend_data(
    trend_data: Dict[str, Any],
    trend_file: Path = DEFAULT_TREND_FILE,
    trends_dir: Path = DEFAULT_TRENDS_DIR
) -> None:
    """
    Write trend data to main file and create daily snapshot.

    Args:
        trend_data: Trend data to write
        trend_file: Path to main trend file
        trends_dir: Path to daily snapshots directory
    """
    # Create directories if needed
    trend_file.parent.mkdir(parents=True, exist_ok=True)
    trends_dir.mkdir(parents=True, exist_ok=True)

    # Write main trend file
    with open(trend_file, 'w') as f:
        json.dump(trend_data, f, indent=2)

    # Create daily snapshot
    today = datetime.now(timezone.utc).strftime("%Y-%m-%d")
    daily_snapshot_path = trends_dir / f"{today}_coverage_trend.json"

    with open(daily_snapshot_path, 'w') as f:
        json.dump(trend_data, f, indent=2)

    print(f"✅ Trend data saved to: {trend_file}")
    print(f"✅ Daily snapshot saved to: {daily_snapshot_path}")


def generate_visualization(trend_data: Dict[str, Any], width: int = 60) -> str:
    """
    Generate ASCII chart showing coverage trend over time.

    Args:
        trend_data: Trend data with history
        width: Chart width in characters

    Returns:
        Formatted ASCII chart string
    """
    if not trend_data["history"]:
        return "No trend data available yet."

    lines = []
    lines.append("")
    lines.append("=" * 80)
    lines.append("COVERAGE TREND VISUALIZATION")
    lines.append("=" * 80)
    lines.append("")

    # Get current coverage
    current = trend_data["current"]["overall_coverage"]
    baseline = trend_data["baseline"]["overall_coverage"]

    # Show summary
    lines.append(f"Baseline:  {baseline:6.2f}%")
    lines.append(f"Current:   {current:6.2f}%")
    lines.append(f"Target:    {TARGET_COVERAGE:6.2f}%")
    lines.append("")

    # Show delta
    if "delta" in trend_data["current"]:
        delta = trend_data["current"]["delta"]
        direction_symbol = {
            "increase": "↑",
            "decrease": "↓",
            "no_change": "→",
            "baseline": "="
        }.get(delta["direction"], "?")

        lines.append(f"Change:    {direction_symbol} {delta['absolute_change']:+6.2f}% ({delta['relative_change']:+.2f}% relative)")
    lines.append("")

    # Generate chart
    lines.append("Coverage History (last 30 snapshots):")
    lines.append("")

    # Find min/max for scaling
    coverages = [s["overall_coverage"] for s in trend_data["history"]]
    min_cov = min(coverages)
    max_cov = max(coverages)

    # Include target in scale if relevant
    if min_cov < TARGET_COVERAGE < max_cov:
        max_cov = max(max_cov, TARGET_COVERAGE)

    # Chart height
    chart_height = 20
    range_cov = max_cov - min_cov if max_cov > min_cov else 1.0

    # Generate chart rows (top to bottom)
    for row in range(chart_height, -1, -1):
        value = min_cov + (range_cov * row / chart_height)

        # Y-axis label
        label = f"{value:5.1f}%"

        # Build chart row
        chart_row = label + " |"

        # Plot each history point
        for snapshot in trend_data["history"]:
            cov = snapshot["overall_coverage"]

            # Check if value is close to this point
            if abs(cov - value) < (range_cov / chart_height):
                # Mark baseline, current, or regular point
                if snapshot == trend_data["baseline"]:
                    chart_row += "B"  # Baseline
                elif snapshot == trend_data["current"]:
                    chart_row += "C"  # Current
                else:
                    chart_row += "*"  # Regular point
            else:
                chart_row += " "

        chart_row += "|"

        # Mark target line
        if abs(TARGET_COVERAGE - value) < (range_cov / chart_height):
            chart_row += " <-- TARGET (80%)"

        lines.append(chart_row)

    # X-axis
    lines.append("       +" + "-" * width + "+")
    lines.append("")
    lines.append("Legend: B = Baseline, C = Current, * = Historical snapshot")
    lines.append("")

    # Show history table
    lines.append("Recent Snapshots:")
    lines.append("")
    lines.append(f"{'Timestamp':<25} {'Commit':<12} {'Coverage':>8} {'Change':>8}")
    lines.append("-" * 60)

    for snapshot in trend_data["history"][-10:]:
        timestamp = snapshot["timestamp"][:19].replace("T", " ")
        commit = (snapshot["commit"] or "unknown")[:10]
        coverage = f"{snapshot['overall_coverage']:.2f}%"

        if "delta" in snapshot:
            delta = snapshot["delta"]["absolute_change"]
            change = f"{delta:+.2f}%"
        else:
            change = "N/A"

        lines.append(f"{timestamp:<25} {commit:<12} {coverage:>8} {change:>8}")

    lines.append("")
    lines.append("=" * 80)

    return "\n".join(lines)


def check_regression(trend_data: Dict[str, Any], threshold: float = REGRESSION_THRESHOLD) -> Tuple[bool, List[str]]:
    """
    Check for coverage regression against recent history.

    Args:
        trend_data: Trend data with history
        threshold: Regression threshold (percentage points)

    Returns:
        Tuple of (has_regression, list of regression messages)
    """
    if len(trend_data["history"]) < 2:
        return False, ["Insufficient history for regression check"]

    messages = []
    current = trend_data["current"]["overall_coverage"]

    # Compare against last 3 snapshots (or all if fewer)
    compare_count = min(3, len(trend_data["history"]) - 1)
    has_regression = False

    for i in range(1, compare_count + 1):
        previous = trend_data["history"][-(i + 1)]
        previous_coverage = previous["overall_coverage"]
        delta = current - previous_coverage

        if delta < -threshold:
            has_regression = True
            commit_msg = previous.get("commit_message", "")[:50]
            messages.append(
                f"REGRESSION: Coverage decreased by {delta:.2f}% "
                f"since {previous['timestamp'][:10]} "
                f"(commit {previous['commit'][:8] if previous.get('commit') else 'unknown'}: '{commit_msg}')"
            )

    # Check module-level regressions
    if trend_data["history"][-1].get("module_breakdown") and trend_data["current"].get("module_breakdown"):
        current_modules = trend_data["current"]["module_breakdown"]
        previous_modules = trend_data["history"][-1]["module_breakdown"]

        for module in current_modules:
            if module in previous_modules:
                delta = current_modules[module] - previous_modules[module]
                if delta < -threshold:
                    has_regression = True
                    messages.append(
                        f"MODULE REGRESSION: {module} decreased by {delta:.2f}% "
                        f"({previous_modules[module]:.2f}% → {current_modules[module]:.2f}%)"
                    )

    if not has_regression:
        messages.append(f"No regression detected (coverage stable or improving)")

    return has_regression, messages


def forecast_target(trend_data: Dict[str, Any], target: float = TARGET_COVERAGE) -> str:
    """
    Forecast when target coverage will be reached based on trend.

    Args:
        trend_data: Trend data with history
        target: Target coverage percentage

    Returns:
        Formatted forecast string
    """
    if len(trend_data["history"]) < 3:
        return "Insufficient history for forecasting (need at least 3 snapshots)"

    current = trend_data["current"]["overall_coverage"]

    if current >= target:
        return f"Target {target}% already reached! Current: {current:.2f}%"

    # Calculate average increase per snapshot (last 5 snapshots)
    recent_snapshots = trend_data["history"][-5:]
    increases = []

    for i in range(1, len(recent_snapshots)):
        delta = recent_snapshots[i]["overall_coverage"] - recent_snapshots[i - 1]["overall_coverage"]
        increases.append(delta)

    avg_increase = sum(increases) / len(increases) if increases else 0

    if avg_increase <= 0:
        return (
            f"Cannot forecast: Coverage trend is flat or decreasing "
            f"(avg change: {avg_increase:.2f}% per snapshot)"
        )

    # Calculate snapshots needed
    remaining = target - current
    snapshots_needed = int(remaining / avg_increase) + 1

    # Estimate dates based on snapshot frequency
    if len(trend_data["history"]) >= 2:
        first_snapshot = datetime.fromisoformat(trend_data["history"][0]["timestamp"].replace("Z", "+00:00"))
        last_snapshot = datetime.fromisoformat(trend_data["current"]["timestamp"].replace("Z", "+00:00"))
        days_span = (last_snapshot - first_snapshot).days
        days_per_snapshot = days_span / (len(trend_data["history"]) - 1) if len(trend_data["history"]) > 1 else 1

        estimated_days = snapshots_needed * days_per_snapshot
        from datetime import timedelta
        estimated_date = last_snapshot + timedelta(days=estimated_days)

        # Generate scenarios
        optimistic_days = estimated_days * 0.7
        realistic_days = estimated_days
        pessimistic_days = estimated_days * 1.3

        optimistic_date = last_snapshot + timedelta(days=optimistic_days)
        pessimistic_date = last_snapshot + timedelta(days=pessimistic_days)

        lines = []
        lines.append("")
        lines.append("=" * 80)
        lines.append(f"COVERAGE FORECAST: {target}% TARGET")
        lines.append("=" * 80)
        lines.append("")
        lines.append(f"Current Coverage: {current:.2f}%")
        lines.append(f"Target Coverage:  {target:.2f}%")
        lines.append(f"Remaining:        {remaining:.2f}%")
        lines.append("")
        lines.append(f"Average Increase: {avg_increase:.2f}% per snapshot")
        lines.append(f"Snapshot Frequency: ~{days_per_snapshot:.1f} days per snapshot")
        lines.append("")
        lines.append(f"Snapshots Needed:  ~{snapshots_needed} snapshots")
        lines.append(f"Estimated Timeline: ~{estimated_days:.0f} days")
        lines.append("")
        lines.append("Scenarios:")
        lines.append(f"  Optimistic:  {optimistic_date.strftime('%Y-%m-%d')} ({optimistic_days:.0f} days)")
        lines.append(f"  Realistic:   {estimated_date.strftime('%Y-%m-%d')} ({realistic_days:.0f} days)")
        lines.append(f"  Pessimistic: {pessimistic_date.strftime('%Y-%m-%d')} ({pessimistic_days:.0f} days)")
        lines.append("")
        lines.append("=" * 80)

        return "\n".join(lines)
    else:
        return f"~{snapshots_needed} snapshots needed (timeline estimation requires more history)"


def record_coverage_ci() -> Dict[str, Any]:
    """
    Record coverage snapshot for CI/CD integration.

    Returns:
        PR comment payload dict
    """
    # Load current coverage
    if not DEFAULT_COVERAGE_FILE.exists():
        return {"error": "Coverage file not found"}

    with open(DEFAULT_COVERAGE_FILE) as f:
        coverage_data = json.load(f)

    # Get commit from environment
    commit_hash = get_git_commit_hash()

    # Record snapshot
    snapshot = record_snapshot(coverage_data, commit_hash)
    trend_data = get_trend_history()
    trend_data = update_trend_data(snapshot, trend_data)
    write_trend_data(trend_data)

    # Generate PR comment payload
    current = snapshot["overall_coverage"]
    baseline = trend_data["baseline"]["overall_coverage"]
    delta = current - baseline

    payload = {
        "title": "Coverage Report",
        "summary": {
            "current": f"{current:.2f}%",
            "baseline": f"{baseline:.2f}%",
            "delta": f"{delta:+.2f}%",
            "target": f"{TARGET_COVERAGE:.2f}%"
        },
        "metrics": {
            "lines_covered": snapshot["covered_lines"],
            "total_lines": snapshot["total_lines"],
            "branch_coverage": snapshot["branch_coverage"]
        },
        "modules": snapshot.get("module_breakdown", {}),
        "trend": "increasing" if delta > 0 else "stable" if delta == 0 else "decreasing",
        "commit": commit_hash[:8] if commit_hash else "unknown"
    }

    return payload


def main():
    """Main execution entry point."""
    parser = argparse.ArgumentParser(
        description="Track coverage trends for Atom v5.0",
        formatter_class=argparse.RawDescriptionHelpFormatter,
        epilog="""
Examples:
  # Record current coverage with visualization
  python coverage_trend_tracker.py --commit $(git rev-parse HEAD) --chart

  # Check for regressions (CI usage)
  python coverage_trend_tracker.py --regression-check

  # Compare two commits
  python coverage_trend_tracker.py --compare-commits abc123 def456

  # Forecast when 80% will be reached
  python coverage_trend_tracker.py --forecast 80

  # Record for CI (generates PR comment payload)
  python coverage_trend_tracker.py --ci-record
        """
    )

    parser.add_argument(
        "--coverage-file",
        type=Path,
        default=DEFAULT_COVERAGE_FILE,
        help="Path to coverage.json (default: backend/tests/coverage_reports/metrics/coverage.json)"
    )
    parser.add_argument(
        "--output",
        type=Path,
        default=DEFAULT_TREND_FILE,
        help="Output path for trend data (default: backend/tests/coverage_reports/metrics/coverage_trend_v5.0.json)"
    )
    parser.add_argument(
        "--commit",
        type=str,
        default=None,
        help="Git commit hash (auto-detected if not provided)"
    )
    parser.add_argument(
        "--chart",
        action="store_true",
        help="Generate ASCII visualization chart"
    )
    parser.add_argument(
        "--regression-check",
        action="store_true",
        help="Check for coverage regressions (exits with 1 on regression)"
    )
    parser.add_argument(
        "--compare-commits",
        nargs=2,
        metavar=("COMMIT1", "COMMIT2"),
        help="Compare coverage between two commits"
    )
    parser.add_argument(
        "--forecast",
        type=float,
        metavar="TARGET",
        default=None,
        help="Forecast when TARGET coverage will be reached (default: 80.0%%)"
    )
    parser.add_argument(
        "--ci-record",
        action="store_true",
        help="Record snapshot for CI and generate PR comment payload"
    )

    args = parser.parse_args()

    # Load coverage data
    if not args.coverage_file.exists():
        print(f"Error: Coverage file not found: {args.coverage_file}", file=sys.stderr)
        sys.exit(1)

    with open(args.coverage_file) as f:
        coverage_data = json.load(f)

    # CI record mode
    if args.ci_record:
        payload = record_coverage_ci()
        print(json.dumps(payload, indent=2))
        sys.exit(0)

    # Get or create trend data
    trend_data = get_trend_history(args.output)

    # Record snapshot
    snapshot = record_snapshot(coverage_data, args.commit)
    trend_data = update_trend_data(snapshot, trend_data)
    write_trend_data(trend_data, args.output)

    # Print summary
    print("")
    print("=" * 80)
    print("COVERAGE SNAPSHOT RECORDED")
    print("=" * 80)
    print(f"Timestamp:    {snapshot['timestamp']}")
    print(f"Commit:       {snapshot['commit'] or 'unknown'}")
    print(f"Coverage:     {snapshot['overall_coverage']:.2f}%")
    print(f"Lines:        {snapshot['covered_lines']:,} / {snapshot['total_lines']:,}")
    print(f"Branch:       {snapshot['branch_coverage']:.2f}%")
    print("")

    if "delta" in snapshot:
        delta = snapshot["delta"]
        symbol = {"increase": "↑", "decrease": "↓", "no_change": "→", "baseline": "="}[delta["direction"]]
        print(f"Change:       {symbol} {delta['absolute_change']:+.2f}% ({delta['relative_change']:+.2f}% relative)")

    print("")
    print("Module Breakdown:")
    for module, pct in snapshot.get("module_breakdown", {}).items():
        print(f"  {module}: {pct:.2f}%")

    print("")

    # Generate chart if requested
    if args.chart:
        print(generate_visualization(trend_data))

    # Regression check
    if args.regression_check:
        has_regression, messages = check_regression(trend_data)

        print("=" * 80)
        print("REGRESSION CHECK")
        print("=" * 80)
        for msg in messages:
            print(msg)
        print("")

        if has_regression:
            print("❌ REGRESSION DETECTED")
            sys.exit(1)
        else:
            print("✅ No regression")
            sys.exit(0)

    # Forecast
    if args.forecast:
        print(forecast_target(trend_data, args.forecast))

    return 0


if __name__ == "__main__":
    sys.exit(main())