debtmap 0.17.0

Code complexity and technical debt analyzer
Documentation
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
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
# Compare Analysis

The `compare` command enables you to track technical debt changes over time by comparing two analysis results. This is essential for validating refactoring efforts, detecting regressions in pull requests, and monitoring project health trends.

## Implementation Status

**All Features Available Now**:
- ✅ Target location tracking with intelligent fuzzy matching
- ✅ Detailed improvement percentage calculations (per-item)
- ✅ Multiple output formats (JSON, Markdown, Terminal)
- ✅ Implementation plan parsing for target extraction
- ✅ Four match strategies (Exact, FunctionLevel, ApproximateName, FileLevel)
- ✅ Resolved items tracking (debt eliminated)
- ✅ Improved items detection (score reduction ≥ 30%)
- ✅ New critical items detection (regressions)
- ✅ Project health metrics and trends
- ✅ CI/CD integration support

## Overview

The compare command analyzes differences between "before" and "after" debtmap analyses, providing:

- **Target location tracking** - Monitor specific code locations through refactoring with fuzzy matching
- **Validation tracking** - Verify debt items are resolved or improved
- **Project health metrics** - Track overall debt trends across your codebase
- **Regression detection** - Identify new critical debt items introduced (score ≥ 60.0)
- **Improvement tracking** - Measure and celebrate debt reduction with detailed per-item metrics
- **CI/CD integration** - Automate quality gates in your pipeline

## Basic Usage

### Command Syntax

```bash
debtmap compare \
  --before path/to/before.json \
  --after path/to/after.json \
  --output validation.json
```

### Command-Line Options

| Option | Required | Description |
|--------|----------|-------------|
| `--before FILE` | Yes | Path to "before" analysis JSON |
| `--after FILE` | Yes | Path to "after" analysis JSON |
| `--output FILE` | No | Output file path (default: stdout) |
| `--plan FILE` | No | Implementation plan to extract target location |
| `--target-location LOCATION` | No | Manual target location (format: `file:function:line`) |
| `--format FORMAT` | No | Output format: `json`, `markdown`, or `terminal` (default: json) |

All comparison features are available now, including target location tracking, fuzzy matching, and multiple output formats.

## Target Location Tracking

Target location tracking allows you to monitor specific code locations through refactoring changes. The compare command uses intelligent fuzzy matching to find your target even when code is moved or renamed.

### Location Format

Target locations use the format: `file:function:line`

Examples:
- `src/main.rs:complex_function:42`
- `lib/parser.rs:parse_expression:156`
- `api/handler.rs:process_request:89`

**Location Format Variations** (src/comparison/location_matcher.rs:12-19):

| Pattern | Description | Example |
|---------|-------------|---------|
| `file:function:line` | Exact match | `src/main.rs:process:42` |
| `file:function` | Function-level match (any line) | `src/main.rs:process` |
| `file:*:line` | Line range match (any function at line) | `src/main.rs:*:42` |
| `file` | File-level match (all items in file) | `src/main.rs` |

The `file:*:line` format is useful when you want to target a specific line number regardless of the function name, which is helpful when function names change during refactoring.

### Specifying Target Locations

#### Option 1: Via Implementation Plan

Create an `IMPLEMENTATION_PLAN.md` file with a target location section:

```markdown
# Implementation Plan

## Target Item
**Location**: ./src/example.rs:complex_function:45
**Current Debt Score**: 85.5
**Severity**: critical

## Problem Analysis
The `complex_function` has high cognitive complexity...

## Proposed Solution
1. Extract nested conditionals into separate functions
2. Use early returns to reduce nesting depth
3. Add comprehensive unit tests
```

Then run compare with the plan:

```bash
debtmap compare --before before.json --after after.json --plan IMPLEMENTATION_PLAN.md
```

#### Option 2: Manual Target Location

Specify the target directly via command-line:

```bash
debtmap compare \
  --before before.json \
  --after after.json \
  --target-location "src/example.rs:complex_function:45"
```

### Matching Strategies

Debtmap uses intelligent matching to find your target item even when code changes. The matcher tries multiple strategies in order, using the most precise match available:

| Strategy | When Used | Confidence |
|----------|-----------|------------|
| **Exact** | `file:function:line` matches exactly | 1.0 |
| **FunctionLevel** | `file:function` matches (any line) | 0.8 |
| **ApproximateName** | Fuzzy name matching finds similar function | 0.6 |
| **FileLevel** | All items in file match | 0.4 |

The comparison result includes the match strategy and confidence score used, along with the count of matched items (useful when fuzzy matching finds multiple candidates).

### Target Status Values

After comparing, the target item will have one of these statuses:

- **Resolved** - Item no longer exists in after analysis (debt eliminated!)
- **Improved** - Item exists but with lower debt score
- **Unchanged** - Item exists with similar metrics (within 5%)
- **Regressed** - Item exists but got worse
- **NotFoundBefore** - Item didn't exist in before analysis
- **NotFound** - Item not found in either analysis

## Project Health Metrics

The compare command tracks project-wide health metrics to show overall trends.

### Tracked Metrics

```json
{
  "project_health": {
    "before": {
      "total_debt_score": 450.5,
      "total_items": 25,
      "critical_items": 5,
      "high_priority_items": 12,
      "average_score": 18.02
    },
    "after": {
      "total_debt_score": 380.2,
      "total_items": 22,
      "critical_items": 3,
      "high_priority_items": 10,
      "average_score": 17.28
    },
    "changes": {
      "debt_score_change": -70.3,
      "debt_score_change_pct": -15.6,
      "items_change": -3,
      "critical_items_change": -2
    }
  }
}
```

### Understanding Metrics

- **total_debt_score** - Sum of all debt item scores
- **total_items** - Total number of debt items detected
- **critical_items** - Items with score ≥ 60.0 (critical threshold)
- **high_priority_items** - Items with score ≥ 40.0 (high priority threshold)
- **average_score** - Mean debt score across all items
- **debt_score_change** - Absolute change in total debt
- **debt_score_change_pct** - Percentage change in total debt

### Debt Trends

The comparison calculates an overall debt trend based on the percentage change:

- **Improving** - Debt decreased by more than 5%
- **Stable** - Debt changed by less than 5% (within normal variance)
- **Regressing** - Debt increased by more than 5%

## Regression Detection

Regressions are new critical debt items (score ≥ 60.0) that appear in the after analysis.

### What Counts as a Regression

A regression is detected when:
1. An item exists in the after analysis
2. The item does NOT exist in the before analysis
3. The item has a debt score ≥ 60.0 (critical severity threshold)

### Regression Output

The compare command returns a `ComparisonResult` with detailed regression information:

```json
{
  "regressions": [
    {
      "location": "src/new_feature.rs:process_data:23",
      "score": 65.5,
      "debt_type": "high_complexity",
      "description": "Function has cyclomatic complexity of 12 and cognitive complexity of 15"
    }
  ]
}
```

Each regression item includes:
- **location** - Full path with function and line number
- **score** - Debt score (≥ 60.0 for regressions)
- **debt_type** - Type of debt detected (e.g., "high_complexity", "god_object")
- **description** - Human-readable explanation of the issue

### Using Regressions in CI/CD

Fail your CI build if regressions are detected:

```bash
# Run comparison
debtmap compare --before before.json --after after.json --output result.json

# Check for regressions
REGRESSION_COUNT=$(jq '.regressions | length' result.json)

if [ "$REGRESSION_COUNT" -gt 0 ]; then
  echo "❌ Regression detected - $REGRESSION_COUNT new critical debt items found"
  jq '.regressions[]' result.json
  exit 1
fi

# Check overall debt trend
TREND=$(jq -r '.summary.overall_debt_trend' result.json)
if [ "$TREND" = "Regressing" ]; then
  echo "⚠️ Warning: Overall debt is increasing"
fi
```

## Improvement Tracking

The compare command tracks improvements as a list of `ImprovementItem` objects with detailed before/after metrics.

### Improvement Types

The `ImprovementType` enum (src/comparison/types.rs:121-126) defines four improvement categories:

- **Resolved** - Debt item completely eliminated (no longer in after analysis) ✓ *Auto-detected*
- **ScoreReduced** - Overall debt score reduced significantly (≥ 30% reduction) ✓ *Auto-detected*
- **ComplexityReduced** - Cyclomatic or cognitive complexity decreased (defined but not auto-classified)
- **CoverageImproved** - Test coverage increased (defined but not auto-classified)

**Note**: Currently, the comparator automatically detects and classifies `Resolved` and `ScoreReduced` improvements (src/comparison/comparator.rs:134-191). The `ComplexityReduced` and `CoverageImproved` types are defined in the type system but not yet auto-assigned during comparison. Future versions may extend auto-detection to classify complexity and coverage improvements.

### Improvement Items Structure

```json
{
  "improvements": [
    {
      "location": "src/example.rs:complex_function:45",
      "before_score": 68.5,
      "after_score": 35.2,
      "improvement_type": "ScoreReduced"
    },
    {
      "location": "src/legacy.rs:old_code:120",
      "before_score": 72.0,
      "after_score": null,
      "improvement_type": "Resolved"
    },
    {
      "location": "src/utils.rs:helper_function:88",
      "before_score": 45.0,
      "after_score": 28.0,
      "improvement_type": "ComplexityReduced"
    }
  ]
}
```

Each improvement item includes:
- **location** - Full path with function and line number
- **before_score** - Original debt score
- **after_score** - New debt score (null if resolved)
- **improvement_type** - Type of improvement achieved

## Before/After Metrics

When you specify a target location (via `--plan` or `--target-location`), the compare command provides detailed before/after metrics for that specific code location.

### Target Item Comparison

```json
{
  "target_item": {
    "location": "src/example.rs:complex_function:45",
    "match_strategy": "Exact",
    "match_confidence": 1.0,
    "matched_items_count": 1,
    "before": {
      "score": 68.5,
      "cyclomatic_complexity": 8,
      "cognitive_complexity": 15,
      "coverage": 45.0,
      "function_length": 120,
      "nesting_depth": 4
    },
    "after": {
      "score": 35.1,
      "cyclomatic_complexity": 3,
      "cognitive_complexity": 5,
      "coverage": 85.0,
      "function_length": 45,
      "nesting_depth": 2
    },
    "improvements": {
      "score_reduction_pct": 48.8,
      "complexity_reduction_pct": 66.7,
      "coverage_improvement_pct": 88.9
    },
    "status": "Improved"
  }
}
```

### Target Metrics Fields

Each `TargetMetrics` object (before/after) includes:
- **score** - Unified debt score
- **cyclomatic_complexity** - Cyclomatic complexity metric
- **cognitive_complexity** - Cognitive complexity metric
- **coverage** - Test coverage percentage
- **function_length** - Lines of code in function
- **nesting_depth** - Maximum nesting depth

### Improvement Percentages

The `improvements` object provides percentage improvements:
- **score_reduction_pct** - Percentage reduction in overall debt score
- **complexity_reduction_pct** - Reduction in cyclomatic/cognitive complexity
- **coverage_improvement_pct** - Increase in test coverage

### Metric Aggregation

When multiple items match the target location (due to fuzzy matching), metrics are aggregated:

- **score** - Average across matched items
- **cyclomatic_complexity** - Average
- **cognitive_complexity** - Average
- **coverage** - Average
- **function_length** - Average
- **nesting_depth** - Maximum (worst case)

The `matched_items_count` field tells you how many items were aggregated.

### Validating Refactoring Success

Use the comparison output to verify your refactoring:

```bash
# Check target status
STATUS=$(jq -r '.target_item.status' result.json)
SCORE_REDUCTION=$(jq '.target_item.improvements.score_reduction_pct' result.json)

echo "Target Status: $STATUS"
echo "Score Reduction: ${SCORE_REDUCTION}%"

# Check for improvements
IMPROVEMENT_COUNT=$(jq '.improvements | length' result.json)
echo "Improvements: $IMPROVEMENT_COUNT items"

# Verify no regressions
REGRESSION_COUNT=$(jq '.regressions | length' result.json)
if [ "$REGRESSION_COUNT" -eq 0 ]; then
  echo "✅ No regressions detected!"
else
  echo "⚠️ $REGRESSION_COUNT new critical items"
fi
```

## Output Formats

### JSON Format

The default JSON format provides complete comparison results:

```bash
debtmap compare --before before.json --after after.json --output result.json
```

The `ComparisonResult` JSON output (src/comparison/types.rs:3-22) includes:
- `metadata` - Comparison metadata (timestamp, file paths, target location)
- `target_item` - Target item comparison with before/after metrics (if specified)
- `project_health` - Project-wide health metrics comparison
- `regressions` - List of new critical items (score ≥ 60.0)
- `improvements` - List of improved/resolved items (≥30% score reduction or resolved)
- `summary` - Summary statistics and overall debt trend

Example output:
```json
{
  "metadata": {
    "comparison_date": "2024-01-15T10:30:00Z",
    "before_file": "before.json",
    "after_file": "after.json",
    "target_location": "src/example.rs:complex_function:45"
  },
  "target_item": {
    "status": "Improved",
    "improvements": {
      "score_reduction_pct": 48.8
    }
  },
  "summary": {
    "target_improved": true,
    "new_critical_count": 0,
    "resolved_count": 3,
    "overall_debt_trend": "Improving"
  }
}
```

### Markdown Format

Generate human-readable markdown reports for pull request comments:

```bash
debtmap compare --before before.json --after after.json --format markdown
```

**Implementation**: src/io/writers/markdown/mod.rs, src/main.rs:1027-1072

The markdown output is suitable for:
- Pull request comments
- Documentation
- Email reports
- Team dashboards

### Terminal Format

Display colorized output directly in the terminal:

```bash
debtmap compare --before before.json --after after.json --format terminal
```

**Implementation**: src/io/writers/terminal.rs, src/main.rs:1011

The terminal format provides:
- Color-coded status indicators
- Formatted tables for metrics
- Human-readable summaries
- Easy scanning of results

## CI/CD Integration

### GitHub Actions Example

```yaml
name: Technical Debt Check

on: [pull_request]

jobs:
  debt-check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0  # Need history for before/after

      - name: Install debtmap
        run: cargo install debtmap

      - name: Analyze main branch
        run: |
          git checkout main
          debtmap analyze . --format json --output before.json

      - name: Analyze PR branch
        run: |
          git checkout ${{ github.head_ref }}
          debtmap analyze . --format json --output after.json

      - name: Compare analyses
        run: |
          debtmap compare \
            --before before.json \
            --after after.json \
            --output comparison.json

      - name: Check comparison result
        run: |
          TREND=$(jq -r '.summary.overall_debt_trend' comparison.json)
          REGRESSION_COUNT=$(jq '.regressions | length' comparison.json)
          IMPROVEMENT_COUNT=$(jq '.improvements | length' comparison.json)

          echo "Debt Trend: $TREND"
          echo "Regressions: $REGRESSION_COUNT"
          echo "Improvements: $IMPROVEMENT_COUNT"

          # Fail on regression
          if [ "$REGRESSION_COUNT" -gt 0 ]; then
            echo "❌ Regression detected"
            jq '.regressions[]' comparison.json
            exit 1
          fi

          # Warn if debt is increasing
          if [ "$TREND" = "Regressing" ]; then
            echo "⚠️ Warning: Overall debt is increasing"
          fi

      - name: Post comparison to PR
        uses: actions/github-script@v6
        with:
          script: |
            const fs = require('fs');
            const comparison = JSON.parse(fs.readFileSync('comparison.json', 'utf8'));

            const body = `## Technical Debt Comparison

            **Overall Trend:** ${comparison.summary.overall_debt_trend}
            **Regressions:** ${comparison.summary.new_critical_count}
            **Improvements:** ${comparison.summary.resolved_count}

            ${comparison.improvements.length > 0 ? `
            ### Improvements
            ${comparison.improvements.map(i => `- ${i.location}: ${i.before_score.toFixed(1)} → ${i.after_score ? i.after_score.toFixed(1) : 'resolved'}`).join('\n')}
            ` : ''}

            ${comparison.regressions.length > 0 ? `
            ### ⚠️ Regressions
            ${comparison.regressions.map(r => `- ${r.location}: ${r.score.toFixed(1)} (${r.debt_type})`).join('\n')}
            ` : ''}`;

            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: body
            });
```

### GitLab CI Example

```yaml
debt_check:
  stage: test
  script:
    # Analyze main branch
    - git fetch origin main
    - git checkout origin/main
    - debtmap analyze . --format json --output before.json

    # Analyze current branch
    - git checkout $CI_COMMIT_SHA
    - debtmap analyze . --format json --output after.json

    # Compare and check status
    - debtmap compare --before before.json --after after.json --output comparison.json
    - |
      TREND=$(jq -r '.summary.overall_debt_trend' comparison.json)
      REGRESSION_COUNT=$(jq '.regressions | length' comparison.json)

      echo "Debt Trend: $TREND"
      echo "Regressions: $REGRESSION_COUNT"

      if [ "$REGRESSION_COUNT" -gt 0 ]; then
        echo "Failed: Regression detected"
        jq '.regressions[]' comparison.json
        exit 1
      fi
  artifacts:
    paths:
      - before.json
      - after.json
      - comparison.json
    expire_in: 1 week
```

### Best Practices for CI/CD

1. **Store analyses as artifacts** - Keep before/after JSON for debugging
2. **Check status field** - Use `status` to determine pass/fail
3. **Track completion percentage** - Monitor progress toward debt resolution
4. **Review improvements** - Celebrate and document successful refactorings
5. **Act on remaining issues** - Create follow-up tasks for unresolved items
6. **Set completion thresholds** - Require minimum completion percentage for merges

## Practical Examples

### Example 1: Basic Comparison

Compare two analyses to track debt changes:

```bash
# Run before analysis
debtmap analyze . --format json --output before.json

# Make changes to codebase...

# Run after analysis
debtmap analyze . --format json --output after.json

# Compare
debtmap compare --before before.json --after after.json --output comparison.json

# Check results
cat comparison.json | jq '.'
# Output shows: target_item, project_health, regressions, improvements, summary
```

### Example 2: Validating Function Refactoring

Validate your refactoring work with target location tracking:

```bash
# Run before analysis
debtmap analyze . --format json --output before.json

# Identify critical items to fix
jq '.items[] | select(.unified_score.final_score >= 60.0)' before.json

# Refactor the high-priority functions...

# Run after analysis
debtmap analyze . --format json --output after.json

# Compare and validate with target location
debtmap compare \
  --before before.json \
  --after after.json \
  --target-location "src/example.rs:complex_function:45" \
  --output comparison.json

# Check target status
STATUS=$(jq -r '.target_item.status' comparison.json)
SCORE_REDUCTION=$(jq '.target_item.improvements.score_reduction_pct' comparison.json)

echo "Target Status: $STATUS"
echo "Score Reduction: ${SCORE_REDUCTION}%"

# Review all improvements
jq '.improvements[]' comparison.json
```

### Example 3: Detecting PR Regressions

Check if a pull request introduces new critical debt:

```bash
# Analyze base branch
git checkout main
debtmap analyze . --format json --output main.json

# Analyze PR branch
git checkout feature/new-feature
debtmap analyze . --format json --output feature.json

# Compare
debtmap compare \
  --before main.json \
  --after feature.json \
  --output comparison.json

# Check for regressions
REGRESSION_COUNT=$(jq '.regressions | length' comparison.json)
TREND=$(jq -r '.summary.overall_debt_trend' comparison.json)

echo "Regressions: $REGRESSION_COUNT"
echo "Debt Trend: $TREND"

# Example output structure:
jq '.' comparison.json
# {
#   "summary": {
#     "overall_debt_trend": "Improving",  // or "Regressing"
#     "new_critical_count": 0,
#     "resolved_count": 3
#   },
#   "regressions": [],
#   "improvements": [...],
#   "project_health": {...}
# }
```

### Example 4: Monitoring Project Health Over Releases

Track overall project health across releases:

```bash
# Analyze release v1.0
git checkout v1.0
debtmap analyze . --format json --output v1.0.json

# Analyze release v1.1
git checkout v1.1
debtmap analyze . --format json --output v1.1.json

# Compare
debtmap compare \
  --before v1.0.json \
  --after v1.1.json \
  --output v1.0-to-v1.1.json

# Check project health metrics
echo "Before (v1.0):"
jq '.project_health.before' v1.0-to-v1.1.json

echo "After (v1.1):"
jq '.project_health.after' v1.0-to-v1.1.json

# Check overall trend
TREND=$(jq -r '.summary.overall_debt_trend' v1.0-to-v1.1.json)
DEBT_CHANGE=$(jq '.project_health.changes.debt_score_change_pct' v1.0-to-v1.1.json)
echo "Debt Trend: $TREND"
echo "Debt Score Change: ${DEBT_CHANGE}%"
```

### Example 5: Full CI/CD Workflow

Complete workflow for continuous debt monitoring:

```bash
#!/bin/bash
# ci-debt-check.sh

set -e

BEFORE="before.json"
AFTER="after.json"
COMPARISON="comparison.json"

# Step 1: Analyze baseline (main branch)
echo "📊 Analyzing baseline..."
git checkout main
debtmap analyze . --format json --output "$BEFORE"

# Step 2: Analyze current branch
echo "📊 Analyzing current branch..."
git checkout -
debtmap analyze . --format json --output "$AFTER"

# Step 3: Run comparison
echo "🔍 Running comparison..."
debtmap compare \
  --before "$BEFORE" \
  --after "$AFTER" \
  --output "$COMPARISON"

# Step 4: Extract metrics
TREND=$(jq -r '.summary.overall_debt_trend' "$COMPARISON")
REGRESSION_COUNT=$(jq '.regressions | length' "$COMPARISON")
IMPROVEMENT_COUNT=$(jq '.improvements | length' "$COMPARISON")
RESOLVED_COUNT=$(jq '.summary.resolved_count' "$COMPARISON")

echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "📈 Debt Comparison Results"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"
echo "Trend: $TREND"
echo "Regressions: $REGRESSION_COUNT"
echo "Improvements: $IMPROVEMENT_COUNT"
echo "Resolved: $RESOLVED_COUNT"
echo "━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━"

# Step 5: Quality gate
if [ "$REGRESSION_COUNT" -gt 0 ]; then
  echo "❌ FAILED: Regression detected"
  jq '.regressions[]' "$COMPARISON"
  exit 1
fi

if [ "$TREND" = "Regressing" ]; then
  echo "⚠️  WARNING: Overall debt is increasing"
  # Don't fail, just warn
fi

if [ "$RESOLVED_COUNT" -gt 0 ]; then
  echo "🎉 SUCCESS: $RESOLVED_COUNT debt items resolved!"
fi

echo "✅ PASSED: No regressions detected"
```

### Example 6: Interpreting Comparison Results

Understanding the comparison output:

```bash
# Run comparison
debtmap compare --before before.json --after after.json --output comparison.json

# Check debt trend
TREND=$(jq -r '.summary.overall_debt_trend' comparison.json)
REGRESSION_COUNT=$(jq '.regressions | length' comparison.json)
IMPROVEMENT_COUNT=$(jq '.improvements | length' comparison.json)

case "$TREND" in
  "Improving")
    echo "🎉 Success! Debt is decreasing"
    echo "Improvements: $IMPROVEMENT_COUNT"
    jq '.improvements[] | "\(.location): \(.improvement_type)"' comparison.json
    ;;
  "Stable")
    echo "➡️  Stable - no significant debt change"
    echo "Improvements: $IMPROVEMENT_COUNT"
    echo "Regressions: $REGRESSION_COUNT"
    ;;
  "Regressing")
    echo "❌ Warning! Debt is increasing"
    echo "New critical items: $REGRESSION_COUNT"
    jq '.regressions[] | "\(.location): \(.score) (\(.debt_type))"' comparison.json
    ;;
esac

# Check if target improved (if target was specified)
if jq -e '.target_item' comparison.json > /dev/null; then
  TARGET_STATUS=$(jq -r '.target_item.status' comparison.json)
  echo "Target Status: $TARGET_STATUS"
fi
```

## Troubleshooting

### Understanding Debt Trends

**Issue**: Confused about what the debt trend means

**Solution**: Check the `summary.overall_debt_trend` field in comparison output:

- `Improving` - Total debt decreased by more than 5%
- `Stable` - Total debt changed by less than 5% (within normal variance)
- `Regressing` - Total debt increased by more than 5%

**Check the trend**:
```bash
TREND=$(jq -r '.summary.overall_debt_trend' comparison.json)
DEBT_CHANGE=$(jq '.project_health.changes.debt_score_change_pct' comparison.json)
echo "Debt Trend: $TREND (${DEBT_CHANGE}% change)"
```

### No Improvements Detected

**Issue**: Made changes but comparison shows no improvements

**Possible causes**:
1. Changes didn't reduce debt scores by ≥30% (improvement threshold)
2. Refactored items had scores <60.0 (not tracked as critical)
3. Changes were neutral (e.g., code moved but complexity unchanged)

**Solution**: Check the details:
```bash
# Compare before/after project health
jq '.project_health.before' result.json
jq '.project_health.after' result.json

# Look for critical items in before analysis
jq '.items[] | select(.unified_score.final_score >= 60.0)' before.json
```

### JSON Parsing Errors

**Problem**: `Error parsing JSON file`

**Solutions**:
1. Verify the file is valid JSON: `jq . before.json`
2. Ensure the file is a debtmap analysis output
3. Check file permissions and path
4. Regenerate the analysis if corrupted

### Understanding Target Status Values

| Status | Meaning | Action Required |
|--------|---------|-----------------|
| `Resolved` | Item eliminated completely | ✅ Celebrate! Item no longer exists |
| `Improved` | Score reduced significantly | ✅ Good progress, verify metrics improved |
| `Unchanged` | No significant change | ⚠️ Review approach, may need different strategy |
| `Regressed` | Item got worse | ❌ Investigate and fix before merging |
| `NotFoundBefore` | Item didn't exist before | ℹ️ New code, ensure quality is acceptable |
| `NotFound` | Item not found in either | ⚠️ Check target location format |

### Handling Missing Files

**Problem**: `No such file or directory`

**Solutions**:
```bash
# Verify files exist
ls -la before.json after.json

# Check current directory
pwd

# Use absolute paths if needed
debtmap compare \
  --before /absolute/path/to/before.json \
  --after /absolute/path/to/after.json
```

### Interpreting Edge Cases

**All Items Resolved**:
```json
{
  "summary": {
    "resolved_count": 25,
    "new_critical_count": 0,
    "overall_debt_trend": "Improving"
  },
  "project_health": {
    "after": {
      "total_items": 0,
      "critical_items": 0
    }
  }
}
```
All debt items resolved - excellent work!

**New Project (Empty Before)**:
```json
{
  "summary": {
    "new_critical_count": 15,
    "resolved_count": 0,
    "overall_debt_trend": "Stable"
  },
  "project_health": {
    "before": {
      "total_items": 0
    }
  }
}
```
New project or first analysis - establish baseline for future comparisons.

**No Changes**:
```json
{
  "summary": {
    "overall_debt_trend": "Stable",
    "new_critical_count": 0,
    "resolved_count": 0
  },
  "improvements": [],
  "regressions": []
}
```
No changes detected - either no code changes or changes were neutral to debt.

## Related Documentation

- [Validation Gates]validation-gates.md - Quality gate thresholds and validation strategies
- [Prodigy Integration]prodigy-integration.md - Automated refactoring workflows with compare validation
- [Output Formats]output-formats.md - Understanding analysis JSON structure and formatters
- [Scoring Strategies]scoring-strategies.md - How debt scores are calculated (critical ≥ 60.0, high priority ≥ 40.0)
- [Threshold Configuration]threshold-configuration.md - Configuring severity thresholds for your project

## Summary

The compare command provides validation for refactoring efforts:

**Current Capabilities:**
- ✅ Target location tracking with intelligent fuzzy matching (4 strategies: Exact, FunctionLevel, ApproximateName, FileLevel)
- ✅ Detect regressions (new critical items with score ≥ 60.0)
- ✅ Track resolved items and improvements (≥30% score reduction, auto-detected)
- ✅ Detailed per-item improvement metrics with before/after scores
- ✅ Multiple output formats (JSON, Markdown, Terminal)
- ✅ Implementation plan parsing for target extraction (via `--plan` flag)
- ✅ Project-wide health metrics and debt trends
- ✅ Automate quality gates in CI/CD pipelines

**Note**: The compare command is fully implemented with all features available. For improvement classification, `Resolved` and `ScoreReduced` types are automatically detected; `ComplexityReduced` and `CoverageImproved` are defined for future use.

Use the compare command regularly to maintain visibility into your codebase's technical health and ensure continuous improvement. All features are fully implemented and ready for production use.