pmat 3.11.0

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
# Emit-Refactor Engine

## Overview

The Emit-Refactor Engine is a dual-mode system that combines real-time defect emission with interactive refactoring capabilities. It provides both high-performance monitoring for CI/CD pipelines and an agent-friendly interface for AI-assisted refactoring.

## Architecture

### Dual-Mode Design

```
┌─────────────────────────────────────────────────────────┐
│                   UnifiedEngine                         │
├─────────────────────────────────────────────────────────┤
│  ┌─────────────────┐         ┌────────────────────┐    │
│  │  Server Mode    │         │  Interactive Mode  │    │
│  ├─────────────────┤         ├────────────────────┤    │
│  │ • Real-time     │         │ • JSON protocol    │    │
│  │ • <5ms latency  │         │ • State machine    │    │
│  │ • Ring buffer   │         │ • Checkpointing    │    │
│  │ • Auto-emit     │         │ • Agent-friendly   │    │
│  └─────────────────┘         └────────────────────┘    │
└─────────────────────────────────────────────────────────┘
```

### State Machine

The refactoring process follows a deterministic state machine:

```mermaid
stateDiagram-v2
    [*] --> Scan: Initialize
    Scan --> Analyze: Next file
    Analyze --> Plan: Violations found
    Plan --> Refactor: Select operation
    Refactor --> Test: Apply changes
    Test --> Lint: Tests pass
    Lint --> Emit: Quality check
    Emit --> Checkpoint: Save state
    Checkpoint --> Analyze: Continue
    Checkpoint --> Complete: All done
    Complete --> [*]
```

## Usage

### Server Mode (Real-time Monitoring)

```bash
# Start server with default config
pmat refactor serve

# Custom configuration
pmat refactor serve \
  --emit-threshold 15 \
  --latency-target 3ms \
  --buffer-size 1000

# With specific config file
pmat refactor serve --config refactor-config.json
```

**Example Configuration:**
```json
{
  "target_complexity": 20,
  "remove_satd": true,
  "max_function_lines": 50,
  "thresholds": {
    "cyclomatic_warn": 10,
    "cyclomatic_error": 20,
    "cognitive_warn": 15,
    "cognitive_error": 30,
    "tdg_warn": 1.5,
    "tdg_error": 2.0
  },
  "strategies": {
    "prefer_functional": true,
    "use_early_returns": true,
    "extract_helpers": true
  }
}
```

### Interactive Mode (Agent Integration)

```bash
# Start interactive session
pmat refactor interactive \
  --project-path . \
  --target-complexity 20 \
  --explain verbose

# Resume from checkpoint
pmat refactor resume --checkpoint refactor_state.json

# Check current status
pmat refactor status
```

## API Reference

### State Types

```rust
pub enum State {
    Scan { targets: Vec<PathBuf> },
    Analyze { current: FileId },
    Plan { violations: Vec<Violation> },
    Refactor { operation: RefactorOp },
    Test { command: String },
    Lint { strict: bool },
    Emit { payload: DefectPayload },
    Checkpoint { reason: String },
    Complete { summary: Summary },
}
```

### Refactoring Operations

```rust
pub enum RefactorOp {
    ExtractFunction {
        name: String,
        start: BytePos,
        end: BytePos,
        params: Vec<String>,
    },
    FlattenNesting {
        function: String,
        strategy: NestingStrategy,
    },
    ReplaceHashMap {
        imports: Vec<String>,
        replacements: Vec<(String, String)>,
    },
    RemoveSatd {
        location: Location,
        fix: SatdFix,
    },
    SimplifyExpression {
        expr: String,
        simplified: String,
    },
}
```

### Interactive Protocol

The interactive mode communicates via JSON messages:

**State Output:**
```json
{
  "state": {
    "state_type": "Analyze",
    "current_file": "./src/main.rs",
    "current_function": "process_data",
    "line_range": [45, 120]
  },
  "metrics": {
    "before": {
      "complexity": [25, 35],
      "tdg": 2.1,
      "satd": 3
    },
    "projected": {
      "complexity": [12, 18],
      "tdg": 1.2,
      "satd": 0
    }
  },
  "suggestion": {
    "suggestion_type": "ExtractFunction",
    "description": "Extract validation logic into separate function",
    "operations": [{
      "name": "validate_input",
      "lines": [50, 75],
      "complexity_reduction": 8
    }]
  },
  "commands": ["continue", "skip", "rollback", "checkpoint", "explain", "exit"]
}
```

**Commands:**
- `continue`: Apply the suggested refactoring
- `skip`: Skip current file/operation
- `rollback`: Undo last change
- `checkpoint`: Save current state
- `explain`: Get detailed explanation
- `exit`: End session

## Metrics

### Complexity Metrics

| Metric | Description | Threshold |
|--------|-------------|-----------|
| Cyclomatic | Number of linearly independent paths | 10 (warn), 20 (error) |
| Cognitive | Mental effort to understand code | 15 (warn), 30 (error) |
| TDG | Technical Debt Gradient | 1.5 (warn), 2.0 (error) |
| SATD | Self-Admitted Technical Debt comments | 0 (ideal) |

### Performance Metrics

- **Server Mode**: <5ms latency for defect emission
- **Interactive Mode**: <100ms for state transitions
- **Memory Usage**: O(1) with ring buffer
- **Throughput**: 10k+ files/second scan rate

## Integration Examples

### CI/CD Pipeline Integration

```yaml
# .github/workflows/refactor-monitor.yml
name: Refactor Monitoring
on: [push]

jobs:
  monitor:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      
      - name: Start Refactor Server
        run: |
          pmat refactor serve \
            --emit-threshold 15 \
            --output refactor-report.json &
          REFACTOR_PID=$!
          
      - name: Run Tests
        run: make test
        
      - name: Collect Refactor Report
        run: |
          kill $REFACTOR_PID
          cat refactor-report.json
          
      - name: Upload Report
        uses: actions/upload-artifact@v3
        with:
          name: refactor-report
          path: refactor-report.json
```

### AI Agent Integration

```python
import json
import subprocess
import sys

class RefactorAgent:
    def __init__(self):
        self.process = subprocess.Popen(
            ['pmat', 'refactor', 'interactive', '--explain', 'verbose'],
            stdin=subprocess.PIPE,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            text=True
        )
    
    def get_state(self):
        line = self.process.stdout.readline()
        return json.loads(line)
    
    def send_command(self, command):
        self.process.stdin.write(f"{command}\n")
        self.process.stdin.flush()
    
    def refactor_codebase(self):
        while True:
            state = self.get_state()
            
            if state.get('state', {}).get('state_type') == 'Complete':
                break
            
            # AI decision logic
            if self.should_refactor(state):
                self.send_command('continue')
            else:
                self.send_command('skip')
    
    def should_refactor(self, state):
        metrics = state.get('metrics', {})
        before = metrics.get('before', {})
        projected = metrics.get('projected', {})
        
        # Refactor if complexity reduction > 30%
        if before and projected:
            reduction = 1 - (projected['complexity'][0] / before['complexity'][0])
            return reduction > 0.3
        
        return False
```

## Refactoring Strategies

### 1. Extract Function
Identifies cohesive code blocks that can be extracted into separate functions.

**Before:**
```rust
fn process_data(input: &str) -> Result<Data, Error> {
    // Validation logic (20 lines)
    if input.is_empty() {
        return Err(Error::Empty);
    }
    // ... more validation ...
    
    // Parsing logic (30 lines)
    let mut parser = Parser::new();
    // ... parsing ...
    
    // Transformation logic (25 lines)
    let result = transform(parsed);
    // ... more transformation ...
    
    Ok(result)
}
```

**After:**
```rust
fn process_data(input: &str) -> Result<Data, Error> {
    validate_input(input)?;
    let parsed = parse_input(input)?;
    transform_data(parsed)
}

fn validate_input(input: &str) -> Result<(), Error> {
    if input.is_empty() {
        return Err(Error::Empty);
    }
    // ... more validation ...
    Ok(())
}
```

### 2. Flatten Nesting
Reduces deep nesting using early returns and guard clauses.

**Before:**
```rust
fn calculate(x: Option<i32>) -> i32 {
    if let Some(val) = x {
        if val > 0 {
            if val < 100 {
                val * 2
            } else {
                100
            }
        } else {
            0
        }
    } else {
        -1
    }
}
```

**After:**
```rust
fn calculate(x: Option<i32>) -> i32 {
    let val = match x {
        None => return -1,
        Some(v) => v,
    };
    
    if val <= 0 {
        return 0;
    }
    
    if val >= 100 {
        return 100;
    }
    
    val * 2
}
```

### 3. Replace HashMap
Optimizes HashMap usage with more efficient alternatives.

**Before:**
```rust
use std::collections::HashMap;

let mut map = HashMap::new();
map.insert("key", value);
```

**After:**
```rust
use rustc_hash::FxHashMap;

let mut map = FxHashMap::default();
map.insert("key", value);
```

## Best Practices

1. **Start with High-Complexity Files**: Focus on files with complexity > 30
2. **Review Before Applying**: Always review suggested refactorings
3. **Test After Each Change**: Ensure tests pass after refactoring
4. **Use Checkpoints**: Save state regularly for large refactoring sessions
5. **Monitor Performance**: Track complexity reduction over time

## Troubleshooting

### Common Issues

**Q: Server mode isn't emitting defects**
A: Check emit threshold configuration. Default is 15 for cyclomatic complexity.

**Q: Interactive mode is slow**
A: Reduce the number of target files or increase the complexity threshold.

**Q: Checkpoint restore fails**
A: Ensure the codebase hasn't changed significantly since the checkpoint was created.

## Performance Tuning

### Server Mode Optimization

```toml
# High-performance configuration
[server]
latency_target = "3ms"
buffer_size = 10000
emit_threshold = 20
batch_size = 100

[analysis]
incremental = true
cache_size = "1GB"
parallel_workers = 8
```

### Interactive Mode Optimization

```toml
# Agent-friendly configuration
[interactive]
explain_level = "brief"
checkpoint_interval = 10
max_suggestions = 3
timeout = "30s"
```

## Future Roadmap

- **Language Support**: TypeScript, Python, Go analyzers
- **ML-Powered Suggestions**: Learn from accepted/rejected refactorings
- **IDE Integration**: VSCode and IntelliJ plugins
- **Distributed Analysis**: Cluster support for large codebases
- **Automatic Refactoring**: Unattended refactoring with confidence scores