debtmap 0.16.3

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
# LLM Integration Guide

How to use debtmap output in AI coding workflows.

## Overview

Debtmap is designed to provide AI coding assistants with the signals they need to understand and fix technical debt. This guide covers:

1. Output formats optimized for LLMs
2. Context suggestions and how to use them
3. Example workflows for different AI tools
4. Interpreting signals for effective prompts

## Output Formats

### Markdown (Recommended)

The `markdown` format is specifically designed for LLM consumption:

```bash
debtmap analyze . --format markdown --top 5
```

**Output structure:**
```markdown
# Technical Debt Analysis

## Summary
- Total items: 47
- Critical: 3
- High: 12
- Moderate: 20
- Low: 12

## Top Priority Items

### #1 [CRITICAL] parse_complex_input
**Location:** src/parser.rs:38-85
**Score:** 8.9/10

**Signals:**
| Metric | Value | Threshold |
|--------|-------|-----------|
| Cyclomatic | 12 | 10 |
| Cognitive | 18 | 15 |
| Coverage | 0% | 80% |
| Nesting | 4 | 3 |

**Context to read:**
- Primary: src/parser.rs:38-85
- Caller: src/handler.rs:100-120
- Caller: src/api.rs:45-60
- Test: tests/parser_test.rs:50-75

---

### #2 [CRITICAL] validate_auth
...
```

**Why this format works:**
- Consistent structure across all items
- Tables for easy metric comparison
- Context suggestions inline
- Minimal tokens for maximum information

### JSON

For programmatic access and CI/CD integration:

```bash
debtmap analyze . --format json --output debt.json
```

**Structure:**
```json
{
  "version": "1.0",
  "timestamp": "2024-01-15T10:30:00Z",
  "summary": {
    "total_items": 47,
    "by_tier": {
      "critical": 3,
      "high": 12,
      "moderate": 20,
      "low": 12
    },
    "total_loc": 15420
  },
  "items": [
    {
      "rank": 1,
      "id": "parse_complex_input_38",
      "tier": "critical",
      "score": 8.9,
      "location": {
        "file": "src/parser.rs",
        "line_start": 38,
        "line_end": 85,
        "function": "parse_complex_input"
      },
      "metrics": {
        "cyclomatic": 12,
        "cognitive": 18,
        "nesting": 4,
        "loc": 47
      },
      "coverage": {
        "line_percent": 0.0,
        "branch_percent": 0.0
      },
      "context": {
        "primary": "src/parser.rs:38-85",
        "callers": [
          {"file": "src/handler.rs", "lines": "100-120", "calls": 12},
          {"file": "src/api.rs", "lines": "45-60", "calls": 8}
        ],
        "tests": [
          {"file": "tests/parser_test.rs", "lines": "50-75"}
        ]
      }
    }
  ]
}
```

### Terminal

For human exploration (not recommended for AI piping):

```bash
debtmap analyze . --format terminal
```

## Context Suggestions

Each debt item includes a `context` field that tells the AI exactly what code to read:

```
CONTEXT:
├─ Primary: src/parser.rs:38-85 (the debt item)
├─ Caller: src/handler.rs:100-120 (understands usage)
├─ Caller: src/api.rs:45-60 (understands usage)
├─ Callee: src/tokenizer.rs:15-40 (understands dependencies)
└─ Test: tests/parser_test.rs:50-75 (understands expected behavior)
```

### Context Types

| Type | What It Contains | Why It Matters |
|------|------------------|----------------|
| Primary | The function with debt | Core code to understand/fix |
| Caller | Functions that call this | Usage patterns, constraints |
| Callee | Functions this calls | Dependencies, side effects |
| Test | Related test files | Expected behavior, test patterns |
| Type | Struct/enum definitions | Data structures being used |

### Using Context Effectively

**Minimal context (fastest):**
```bash
# Just get the primary location
debtmap analyze . --format json | jq '.items[0].location'
```

**Full context (most accurate):**
```bash
# Read all suggested files
debtmap analyze . --format markdown --top 1
# Then have the AI read each file in the context section
```

## Example Workflows

### Claude Code Integration

**Direct piping:**
```bash
debtmap analyze . --format markdown --top 3 | claude "Fix the top item. Read the context files first."
```

**Two-step workflow:**
```bash
# Step 1: Get the analysis
debtmap analyze . --format markdown --lcov coverage.lcov --top 5 > debt.md

# Step 2: Send to Claude with context
cat debt.md | claude "
Read the context files for item #1 before making changes.
Then fix the debt item following these rules:
1. Add tests first
2. Refactor only after tests pass
3. Keep functions under 20 lines
"
```

**Focused fix with coverage:**
```bash
# Generate fresh coverage
cargo llvm-cov --lcov --output-path coverage.lcov

# Analyze with coverage
debtmap analyze . --format markdown --lcov coverage.lcov --top 1

# Send the top item to Claude
debtmap analyze . --format markdown --lcov coverage.lcov --top 1 | \
  claude "Add tests for this function to reach 80% coverage"
```

### Cursor Integration

Cursor works best with file-based context:

```bash
# Generate debt report
debtmap analyze . --format markdown --top 10 > .cursor/debt-report.md

# In Cursor, reference the report:
# @debt-report.md Fix the top critical item
```

### Custom Agent Workflow

For building your own AI pipeline:

```python
import json
import subprocess

# Run debtmap analysis
result = subprocess.run(
    ["debtmap", "analyze", ".", "--format", "json", "--top", "10"],
    capture_output=True,
    text=True
)
debt_data = json.loads(result.stdout)

# Process each item
for item in debt_data["items"]:
    # Extract context files
    context_files = []
    context_files.append(item["context"]["primary"])
    context_files.extend([c["file"] for c in item["context"].get("callers", [])])

    # Read context files
    context_content = ""
    for file_spec in context_files:
        file_path, lines = parse_file_spec(file_spec)
        context_content += read_file_lines(file_path, lines)

    # Build prompt
    prompt = f"""
    Fix this technical debt item:

    Location: {item["location"]["file"]}:{item["location"]["line_start"]}
    Function: {item["location"]["function"]}
    Score: {item["score"]}/10

    Signals:
    - Cyclomatic complexity: {item["metrics"]["cyclomatic"]}
    - Test coverage: {item["coverage"]["line_percent"]}%

    Context code:
    {context_content}

    Instructions:
    1. Add tests first
    2. Refactor to reduce complexity
    3. Keep the same public API
    """

    # Send to your LLM
    response = call_llm(prompt)
```

### CI/CD Integration

**GitHub Actions example:**
```yaml
name: Debt Analysis

on: [pull_request]

jobs:
  analyze:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Install debtmap
        run: cargo install debtmap

      - name: Generate coverage
        run: cargo llvm-cov --lcov --output-path coverage.lcov

      - name: Analyze debt
        run: |
          debtmap analyze . --format json --lcov coverage.lcov --output debt.json

      - name: Check for critical items
        run: |
          CRITICAL=$(jq '.summary.by_tier.critical' debt.json)
          if [ "$CRITICAL" -gt 0 ]; then
            echo "::warning::Found $CRITICAL critical debt items"
          fi

      - name: Upload report
        uses: actions/upload-artifact@v3
        with:
          name: debt-report
          path: debt.json
```

## Interpreting Signals

### Severity Score (0-10)

The severity score combines multiple signals:

| Score | Tier | Interpretation |
|-------|------|----------------|
| 8.0-10.0 | Critical | High complexity, no tests, high coupling |
| 5.0-7.9 | High | Moderate risk, coverage gaps |
| 2.0-4.9 | Moderate | Lower risk, monitor |
| 0.0-1.9 | Low | Acceptable state |

### Complexity Signals

**Cyclomatic complexity:**
- 1-5: Simple, easy to test
- 6-10: Moderate, manageable
- 11-20: Complex, consider splitting
- 21+: Very complex, high priority

**Cognitive complexity:**
- Measures how hard code is to understand
- Penalizes nesting more than cyclomatic
- Higher values = harder to reason about

**Nesting depth:**
- 1-2: Normal
- 3: Getting complex
- 4+: Strongly consider refactoring

### Coverage Signals

**Line coverage:**
- 0%: Critical gap, no tests at all
- 1-50%: Poor coverage
- 51-80%: Moderate coverage
- 81%+: Good coverage

**Branch coverage:**
- More important than line coverage
- Missing branches = missing edge cases
- 0% branch = high risk

### Coupling Signals

**Fan-in (callers):**
- High fan-in = many dependents
- Changes affect many places
- Higher priority for stability

**Fan-out (callees):**
- High fan-out = many dependencies
- Complex testing requirements
- Consider dependency injection

## Best Practices

### For Effective AI Prompts

1. **Always include context files**
   - AI makes better decisions with more context
   - Context suggestions are curated for relevance

2. **Specify your constraints**
   - "Keep the same public API"
   - "Add tests before refactoring"
   - "Functions must be under 20 lines"

3. **One item at a time**
   - Focus on top priority item
   - Complete fix before moving on
   - Re-run analysis after changes

4. **Verify with coverage**
   - Regenerate coverage after changes
   - Run debtmap again to confirm improvement
   - Track score reduction

### For CI/CD Integration

1. **Set appropriate thresholds**
   - Don't fail on existing debt
   - Fail on new critical items
   - Track trends over time

2. **Cache analysis results**
   - Use git-based caching
   - Only re-analyze changed files

3. **Integrate with PR comments**
   - Show debt impact of changes
   - Suggest focus areas for review

## Troubleshooting

### Empty context suggestions

**Cause:** Call graph analysis couldn't resolve callers/callees

**Solution:** Ensure file parsing succeeded:
```bash
debtmap analyze . -vv  # Verbose mode shows parsing issues
```

### Inconsistent scores between runs

**Cause:** Non-deterministic analysis (should not happen)

**Solution:** Report as a bug with reproducible example

### Large context suggestions

**Cause:** High coupling in codebase

**Solution:** Use `--max-context-lines` to limit:
```bash
debtmap analyze . --format markdown --max-context-lines 300
```

## API Reference

### CLI Options for LLM Integration

| Option | Description |
|--------|-------------|
| `--format markdown` | LLM-optimized markdown output |
| `--format json` | Structured JSON output |
| `--top N` | Limit to top N items |
| `--lcov FILE` | Include coverage data |
| `--min-score N` | Filter items below score N |
| `--output FILE` | Write to file instead of stdout |

### JSON Schema

The JSON output follows a stable schema. See [Output Formats](output-formats.md) for the complete schema definition.

## Next Steps

- **Configure analysis:** See [Configuration]configuration.md
- **Understand metrics:** See [Metrics Reference]metrics-reference.md
- **View examples:** See [Examples]examples.md