pmat 3.17.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
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
# Claude Code Sub-Agent Scaffolding

**Status**: ✅ IMPLEMENTED (PMAT-7007)
**Version**: v2.144.0
**Category**: Code Quality Tools

## Overview

PMAT now provides comprehensive scaffolding for generating specialized Claude Code sub-agents. These sub-agents are designed to work seamlessly with Claude Code's MCP (Model Context Protocol) integration, providing focused expertise in specific code quality domains.

## What Are Sub-Agents?

Sub-agents are specialized AI assistants that:
- **Focus on specific domains** (complexity analysis, mutation testing, technical debt detection, etc.)
- **Integrate with PMAT MCP tools** via standardized tool mappings
- **Follow Claude Code conventions** for markdown-based definitions
- **Coordinate with each other** through well-defined communication protocols
- **Provide deterministic analysis** powered by PMAT's quality infrastructure

## MVP Sub-Agents (5 Available)

### 1. Complexity Analyst
- **Purpose**: Cyclomatic and cognitive complexity analysis
- **Tools**: `pmat__analyze_complexity`, `pmat__analyze_cognitive_complexity`
- **Use Cases**: Identify high-complexity hotspots, suggest refactorings
- **Invocation**: `@complexity-analyst analyze src/`

### 2. Mutation Tester
- **Purpose**: Mutation testing with ML-powered prediction
- **Tools**: `pmat__mutation_test`, `pmat__mutation_predict`, `pmat__equivalent_detector`
- **Use Cases**: Test quality assessment, equivalent mutant detection
- **Invocation**: `@mutation-tester test src/lib.rs`

### 3. SATD Detector
- **Purpose**: Technical debt tracking (TODO/FIXME/HACK comments)
- **Tools**: `pmat__analyze_satd`, `pmat__analyze_context`
- **Use Cases**: Identify and prioritize technical debt
- **Invocation**: `@satd-detector scan for old debt`

### 4. Dead Code Eliminator
- **Purpose**: Unused code identification with safety scoring
- **Tools**: `pmat__analyze_dead_code`, `pmat__analyze_imports`
- **Use Cases**: Safe code cleanup, bloat reduction
- **Invocation**: `@dead-code-eliminator analyze src/utils --private-only`

### 5. Documentation Enforcer
- **Purpose**: Generic description detection and quality enforcement
- **Tools**: `pmat__check_generic_docs`, `pmat__analyze_context`
- **Use Cases**: Ensure high-quality documentation, eliminate vague descriptions
- **Invocation**: `@documentation-enforcer analyze src/services`

## CLI Commands

### List Available Sub-Agents

```bash
# List MVP sub-agents (default)
pmat scaffold list-subagents

# List all sub-agents (including future phases)
pmat scaffold list-subagents --all
```

**Output**:
```
Available PMAT Sub-Agents:

  ✅ MVP complexity-analyst - Expert in cyclomatic and cognitive complexity analysis
    Tools: analyze_complexity, analyze_cognitive_complexity

  ✅ MVP mutation-tester - Mutation testing specialist with ML prediction
    Tools: mutation_test, mutation_predict, equivalent_detector

  ...

Total: 5 MVP, 0 Future
```

### Create a Specific Sub-Agent

```bash
# Create to current directory/.claude/subagents (default)
pmat scaffold create-subagent complexity-analyst

# Create to custom directory
pmat scaffold create-subagent mutation-tester -o /path/to/output
```

**Output**:
```
Creating sub-agent: complexity-analyst
  Description: Expert in cyclomatic and cognitive complexity analysis
  Output: .claude/subagents

✅ Sub-agent created successfully!
  File: .claude/subagents/complexity-analyst.md

Next steps:
  1. Review the generated sub-agent definition
  2. Customize if needed (prompts, tools, examples)
  3. Use with Claude Code: Place in .claude/subagents/
  4. Invoke: @complexity-analyst <command>
```

### Create All MVP Sub-Agents

```bash
# Create all 5 MVP sub-agents at once
pmat scaffold create-all-subagents

# Create to custom directory
pmat scaffold create-all-subagents -o /path/to/output
```

**Output**:
```
Creating all MVP sub-agents...
  Output: .claude/subagents

✅ Created 5 sub-agents successfully!

  ✅ complexity-analyst - complexity-analyst.md
  ✅ mutation-tester - mutation-tester.md
  ✅ satd-detector - satd-detector.md
  ✅ dead-code-eliminator - dead-code-eliminator.md
  ✅ documentation-enforcer - documentation-enforcer.md

Next steps:
  1. Review generated sub-agents in .claude/subagents
  2. Place them in your project's .claude/subagents/ directory
  3. Use with Claude Code: @<agent-name> <command>

Example usage:
  @complexity-analyst analyze src/
  @mutation-tester test src/lib.rs
  @satd-detector scan for old debt
```

### Validate a Sub-Agent Definition

```bash
pmat scaffold validate-subagent .claude/subagents/complexity-analyst.md
```

**Output (on success)**:
```
Validating sub-agent: .claude/subagents/complexity-analyst.md

✅ Validation passed!
  All required sections present
  Markdown format valid
  No issues found
```

**Output (on failure)**:
```
Validating sub-agent: .claude/subagents/custom-agent.md

❌ Validation failed!

Issues:
  ✗ Missing required section: ## Capabilities
  ✗ Missing required section: ## Tools Used
  ✗ File should start with # title

Warnings:
  ⚠ No MCP tools mentioned (expected for PMAT sub-agents)
  ⚠ Contains placeholder text (TODO/TBD)
```

### Show MCP Tool Mapping

```bash
# Show mapping for all sub-agents
pmat scaffold show-tool-mapping

# Show mapping for specific sub-agent
pmat scaffold show-tool-mapping --agent complexity-analyst
```

**Output**:
```
MCP Tool Mapping: complexity-analyst
  Description: Expert in cyclomatic and cognitive complexity analysis

Primary Tools:
  • pmat__analyze_complexity
  • pmat__analyze_cognitive_complexity
```

### Export Tool Mapping as JSON

```bash
pmat scaffold export-tool-mapping -o tool-mapping.json
```

**Output** (`tool-mapping.json`):
```json
{
  "complexity-analyst": [
    "pmat__analyze_complexity",
    "pmat__analyze_cognitive_complexity"
  ],
  "mutation-tester": [
    "pmat__mutation_test",
    "pmat__mutation_predict",
    "pmat__equivalent_detector"
  ],
  ...
}
```

## Integration with Claude Code

### Step 1: Generate Sub-Agents

```bash
# Generate all MVP sub-agents
cd your-project
pmat scaffold create-all-subagents -o .claude/subagents
```

### Step 2: Configure Claude Code

Ensure your project has `.claude/subagents/` directory:

```bash
ls -la .claude/subagents/
# complexity-analyst.md
# mutation-tester.md
# satd-detector.md
# dead-code-eliminator.md
# documentation-enforcer.md
```

### Step 3: Use Sub-Agents in Claude Code

In Claude Code, invoke sub-agents using `@` syntax:

```
@complexity-analyst analyze src/services/
```

Claude Code will:
1. Load the sub-agent definition from `.claude/subagents/complexity-analyst.md`
2. Invoke PMAT MCP tools (`pmat__analyze_complexity`, etc.)
3. Return analysis in the format specified by the sub-agent

### Example Workflows

#### Complexity Analysis Workflow
```
User: "Is this codebase complex?"
Claude Code: @complexity-analyst analyze src/

ComplexityAnalyst: "Let me analyze the codebase complexity...
[Calls pmat__analyze_complexity via MCP]

Found 12 high-complexity functions (CC >10):
1. src/parser.rs:parse_expression() - CC=18 (CRITICAL)
2. src/validator.rs:validate_schema() - CC=14 (HIGH)
...

Recommendations:
- Extract parse_expression() into smaller functions
- Simplify validate_schema() conditionals"
```

#### Mutation Testing Workflow
```
User: "How good are my tests?"
Claude Code: @mutation-tester assess src/lib.rs

MutationTester: "Running mutation analysis...
[Calls pmat__mutation_test via MCP]

Mutation Score: 82% (41/50 mutants killed)
Test Quality: GOOD

Survived Mutants (9):
1. src/lib.rs:45 - Removed `if` guard (not tested)
2. src/lib.rs:67 - Changed `>` to `>=` (boundary not tested)
...

Recommended Tests:
- Add test for guard condition at line 45
- Add boundary tests for comparison at line 67"
```

#### SATD Detection Workflow
```
User: "What technical debt do we have?"
Claude Code: @satd-detector scan

SATDDetector: "Scanning for technical debt...
[Calls pmat__analyze_satd via MCP]

Found 23 SATD markers:

CRITICAL (3):
- src/auth.rs:89 - TODO: Fix auth bypass vulnerability
- src/db.rs:123 - FIXME: SQL injection risk

HIGH (7):
- src/cache.rs:45 - TODO: Add TTL expiration
- src/api.rs:234 - HACK: Temporary workaround for API rate limit
...

Prioritization:
1. Fix security issues immediately (2 critical)
2. Address 7 high-priority items in Sprint 25
3. Track 13 low-priority for future cleanup"
```

## Sub-Agent Template Structure

Each sub-agent definition follows this standardized markdown format:

### Required Sections

1. **# Title** - Sub-agent name (e.g., "Complexity Analyst")
2. **## Description** - One-sentence purpose
3. **## Capabilities** - Bulleted list of what the agent can do
4. **## Tools Used** - MCP tools with `pmat__` prefix
5. **## Role Definition** - Detailed expertise and constraints
6. **## Communication Protocol** - How to interact with Claude Code and other sub-agents
7. **## Implementation Workflow** - Step-by-step process
8. **## Example Invocations** - Concrete usage examples (automatic, manual, coordination)
9. **## Quality Gates** - Success criteria and thresholds

### Example Template

```markdown
# Complexity Analyst

## Description
Expert in cyclomatic and cognitive complexity analysis, suggests refactorings.

## Capabilities
- Analyze cyclomatic complexity (CC) for functions
- Calculate cognitive complexity for human comprehension
- Identify high-complexity hotspots requiring refactoring
- Suggest specific refactoring strategies

## Tools Used
- `analyze_complexity` (MCP) - Primary complexity analysis
- `analyze_cognitive_complexity` (MCP) - Cognitive load metrics

## Role Definition
You are a complexity analysis expert specializing in identifying
and resolving code complexity issues. You understand:
- Cyclomatic complexity thresholds (CC >10 is high)
- Cognitive complexity patterns (nested conditions, callbacks)
- Refactoring strategies (extract method, simplify conditionals)

**Constraints**:
- Never suggest refactorings without measuring impact
- Always provide before/after complexity scores
- Respect PMAT quality gates (CC ≤8)

...
```

## Customization

### Adding Custom Sub-Agents

1. Create a new markdown file following the template structure:

```bash
cp .claude/subagents/complexity-analyst.md .claude/subagents/my-custom-agent.md
```

2. Edit the file to define your agent:
   - Change title and description
   - Define capabilities
   - Map to PMAT MCP tools (or custom tools)
   - Specify role and workflow

3. Validate the definition:

```bash
pmat scaffold validate-subagent .claude/subagents/my-custom-agent.md
```

4. Use with Claude Code:

```
@my-custom-agent <command>
```

### Extending Existing Sub-Agents

Edit the generated `.md` files to:
- **Add examples** in "Example Invocations" section
- **Customize constraints** in "Role Definition"
- **Adjust thresholds** in "Quality Gates"
- **Add coordination patterns** in "Communication Protocol"

## Future Sub-Agents (Planned)

- **Rust Quality Expert** (P1) - Borrow checker patterns, lifetime analysis
- **Python Quality Expert** (P1) - Type hints, async patterns
- **TypeScript Quality Expert** (P1) - Interface usage, generics
- **Test Coverage Analyst** (P1) - Coverage gaps, test suggestions
- **Refactoring Advisor** (P2) - Pattern matching, best practices
- **Quality Gate Orchestrator** (P2) - Multi-metric aggregation
- **WASM Deep Inspector** (P2) - WebAssembly bytecode analysis

## Best Practices

### 1. Use Sub-Agents for Focused Tasks

✅ **Good**:
```
@complexity-analyst analyze src/parser.rs
@mutation-tester test src/validator.rs
```

❌ **Bad**:
```
@complexity-analyst "analyze everything and generate tests and fix bugs"
```

### 2. Coordinate Multiple Sub-Agents

```
Step 1: @complexity-analyst find high-complexity functions
Step 2: @mutation-tester assess test coverage for those functions
Step 3: @refactoring-advisor suggest improvements
```

### 3. Validate Before Deployment

```bash
# Always validate custom sub-agents
pmat scaffold validate-subagent .claude/subagents/custom.md
```

### 4. Keep Definitions Updated

```bash
# Regenerate when PMAT updates
pmat scaffold create-all-subagents --force
```

## Troubleshooting

### Sub-Agent Not Found

**Error**: `No sub-agent named 'xyz' found`

**Solution**: Check available agents:
```bash
pmat scaffold list-subagents --all
```

### MCP Tool Not Available

**Error**: `Tool pmat__xyz not found`

**Solution**: Ensure PMAT MCP server is running:
```bash
# Start MCP server
pmat serve mcp --port 3000
```

### Validation Failures

**Error**: `Missing required section: ## Capabilities`

**Solution**: Use a template from existing agents:
```bash
pmat scaffold create-subagent complexity-analyst
# Copy and modify
```

## Related Documentation

- [PMAT MCP Integration]./MCP_INTEGRATION.md
- [Quality Gates]./QUALITY_GATES.md
- [Mutation Testing]./MUTATION_TESTING.md
- [Complexity Analysis]./COMPLEXITY_ANALYSIS.md

## Implementation Details

**Files**:
- `server/src/scaffold/agent/subagents.rs` - Core infrastructure
- `server/src/scaffold/agent/subagent_templates/*.md.tmpl` - Templates
- `server/src/cli/handlers/subagent_handlers.rs` - CLI handlers

**Ticket**: PMAT-7007
**Sprint**: 24
**Release**: v2.144.0