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
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
// Complexity analysis handlers (extracted from extended_tools.rs for CB-040)

#[derive(Debug, Deserialize, Serialize)]
struct AnalyzeComplexityArgs {
    project_path: Option<String>,
    toolchain: Option<String>,
    format: Option<String>,
    max_cyclomatic: Option<u16>,
    max_cognitive: Option<u16>,
    include: Option<Vec<String>>,
    top_files: Option<usize>,
}

fn parse_complexity_args(arguments: serde_json::Value) -> Result<AnalyzeComplexityArgs, String> {
    serde_json::from_value(arguments)
        .map_err(|e| format!("Invalid analyze_complexity arguments: {e}"))
}

struct ComplexityAnalysisContext {
    project_path: PathBuf,
    toolchain: String,
    _thresholds: crate::services::complexity::ComplexityThresholds,
}

/// R22-1 / D101: project_path is required. `resolve_project_path_complexity`
/// rejects null/missing/empty values.
/// R22-2 / D102: resolve glob patterns in project_path before toolchain
/// detection so that `src/**` or `crates/*/src/**` produce a concrete
/// directory instead of the "authoritative zeros" pre-R21-4 behavior.
fn prepare_complexity_analysis(
    args: &AnalyzeComplexityArgs,
) -> Result<ComplexityAnalysisContext, String> {
    let project_path = resolve_project_path_complexity(args.project_path.clone())?;
    let toolchain = detect_toolchain(&args.toolchain, &project_path);
    let thresholds = build_complexity_thresholds(args);

    Ok(ComplexityAnalysisContext {
        project_path,
        toolchain,
        _thresholds: thresholds,
    })
}

async fn perform_complexity_analysis(
    context: &ComplexityAnalysisContext,
    args: &AnalyzeComplexityArgs,
) -> (crate::services::complexity::ComplexityReport, usize) {
    use crate::services::complexity::aggregate_results;

    let (file_metrics, file_count) =
        analyze_project_files(&context.project_path, &context.toolchain, args).await;

    let report = aggregate_results(file_metrics);
    (report, file_count)
}

fn generate_complexity_content(
    report: &crate::services::complexity::ComplexityReport,
    file_metrics: &[crate::services::complexity::FileComplexityMetrics],
    args: &AnalyzeComplexityArgs,
) -> String {
    if let Some(top_files_count) = args.top_files {
        if top_files_count > 0 {
            generate_ranked_content(file_metrics, top_files_count, args)
        } else {
            format_complexity_output(report, args)
        }
    } else {
        format_complexity_output(report, args)
    }
}

fn generate_ranked_content(
    file_metrics: &[crate::services::complexity::FileComplexityMetrics],
    top_files_count: usize,
    args: &AnalyzeComplexityArgs,
) -> String {
    use crate::services::ranking::{rank_files_by_complexity, ComplexityRanker};

    let ranker = ComplexityRanker::default();
    let rankings = rank_files_by_complexity(file_metrics, top_files_count, &ranker);
    format_complexity_rankings(&rankings, args)
}

fn build_complexity_response(
    request_id: serde_json::Value,
    content_text: String,
    report: &crate::services::complexity::ComplexityReport,
    toolchain: &str,
    file_count: usize,
    args: &AnalyzeComplexityArgs,
) -> McpResponse {
    let result = json!({
        "content": [{
            "type": "text",
            "text": content_text
        }],
        "report": report,
        "toolchain": toolchain,
        "files_analyzed": file_count,
        "format": args.format.as_deref().unwrap_or("summary"),
        "top_files": args.top_files,
    });

    McpResponse::success(request_id, result)
}

async fn handle_analyze_complexity(
    request_id: serde_json::Value,
    arguments: serde_json::Value,
) -> McpResponse {
    let args = match parse_complexity_args(arguments) {
        Ok(args) => args,
        Err(e) => return McpResponse::error(request_id, -32602, e),
    };

    // R22-1 / D101 + R22-2 / D102: validate project_path (reject null/
    // missing/empty) then glob-expand; fail loud with `-32602` if empty glob.
    let context = match prepare_complexity_analysis(&args) {
        Ok(ctx) => ctx,
        Err(msg) => return McpResponse::error(request_id, -32602, msg),
    };

    info!(
        "Analyzing complexity for {:?} using {} toolchain",
        context.project_path, context.toolchain
    );

    let (file_metrics, file_count) =
        analyze_project_files(&context.project_path, &context.toolchain, &args).await;

    let report = crate::services::complexity::aggregate_results(file_metrics.clone());
    let content_text = generate_complexity_content(&report, &file_metrics, &args);

    build_complexity_response(
        request_id,
        content_text,
        &report,
        &context.toolchain,
        file_count,
        &args,
    )
}

/// R22-1 / D101 + R22-2 / D102: Validate and glob-expand `project_path`.
///
/// D101: reject null/missing/empty values to avoid silently analyzing the
/// server's cwd.
/// D102: expand shell-style globs via the shared `services::path_glob`
/// helper, failing loud on empty expansion.
fn resolve_project_path_complexity(project_path_arg: Option<String>) -> Result<PathBuf, String> {
    let _validated = require_project_path(project_path_arg.clone())?;
    let raw = project_path_arg
        .as_deref()
        .expect("require_project_path returned Ok for None");
    match resolve_project_path_with_globs(raw) {
        ResolvedProjectPath::Concrete(p) => Ok(p),
        e @ ResolvedProjectPath::EmptyGlob(_) => Err(e.into_error_message()),
    }
}


fn detect_toolchain(toolchain_arg: &Option<String>, project_path: &Path) -> String {
    if let Some(t) = toolchain_arg {
        t.clone()
    } else if project_path.join("Cargo.toml").exists() {
        "rust".to_string()
    } else if project_path.join("package.json").exists() || project_path.join("deno.json").exists()
    {
        "deno".to_string()
    } else if project_path.join("pyproject.toml").exists()
        || project_path.join("requirements.txt").exists()
    {
        "python-uv".to_string()
    } else {
        "rust".to_string() // default
    }
}

fn build_complexity_thresholds(
    args: &AnalyzeComplexityArgs,
) -> crate::services::complexity::ComplexityThresholds {
    use crate::services::complexity::ComplexityThresholds;

    let mut thresholds = ComplexityThresholds::default();
    if let Some(max) = args.max_cyclomatic {
        thresholds.cyclomatic_error = max;
        thresholds.cyclomatic_warn = (max * 3 / 4).max(1);
    }
    if let Some(max) = args.max_cognitive {
        thresholds.cognitive_error = max;
        thresholds.cognitive_warn = (max * 3 / 4).max(1);
    }
    thresholds
}

async fn analyze_project_files(
    project_path: &Path,
    toolchain: &str,
    args: &AnalyzeComplexityArgs,
) -> (
    Vec<crate::services::complexity::FileComplexityMetrics>,
    usize,
) {
    use crate::services::file_discovery::ProjectFileDiscovery;

    let mut file_metrics = Vec::with_capacity(256);
    let mut file_count = 0;

    // Use ProjectFileDiscovery which properly respects .gitignore files
    let discovery = ProjectFileDiscovery::new(project_path.to_path_buf());
    let discovered_files = match discovery.discover_files() {
        Ok(files) => files,
        Err(e) => {
            error!("Failed to discover files: {}", e);
            return (file_metrics, file_count);
        }
    };

    for path in discovered_files {
        if path.is_dir() || !should_analyze_file(&path, toolchain) {
            continue;
        }

        if !matches_include_filters(&path, &args.include) {
            continue;
        }

        file_count += 1;

        if let Some(metrics) = analyze_file_complexity(&path, toolchain).await {
            file_metrics.push(metrics);
        }
    }

    (file_metrics, file_count)
}

fn should_analyze_file(path: &Path, toolchain: &str) -> bool {
    match toolchain {
        "rust" => path.extension().and_then(|s| s.to_str()) == Some("rs"),
        "deno" => matches!(
            path.extension().and_then(|s| s.to_str()),
            Some("ts" | "tsx" | "js" | "jsx")
        ),
        "python-uv" => path.extension().and_then(|s| s.to_str()) == Some("py"),
        _ => false,
    }
}

fn matches_include_filters(path: &Path, include_patterns: &Option<Vec<String>>) -> bool {
    let Some(ref patterns) = include_patterns else {
        return true;
    };

    if patterns.is_empty() {
        return true;
    }

    let path_str = path.to_string_lossy();
    patterns
        .iter()
        .any(|pattern| matches_pattern(&path_str, pattern))
}

fn matches_pattern(path_str: &str, pattern: &str) -> bool {
    if pattern.contains("**") {
        // Match any path containing the pattern after **
        let parts: Vec<&str> = pattern.split("**").collect();
        if parts.len() == 2 {
            path_str.contains(parts[1].trim_start_matches('/'))
        } else {
            false
        }
    } else if pattern.starts_with("*.") {
        // Match by extension
        path_str.ends_with(&pattern[1..])
    } else {
        // Direct substring match
        path_str.contains(pattern)
    }
}

async fn analyze_file_complexity(
    path: &Path,
    toolchain: &str,
) -> Option<crate::services::complexity::FileComplexityMetrics> {
    match toolchain {
        "rust" => {
            use crate::services::ast_rust;
            ast_rust::analyze_rust_file_with_complexity(path).await.ok()
        }
        "deno" => {
            #[cfg(feature = "typescript-ast")]
            {
                use crate::services::ast_typescript;
                ast_typescript::analyze_typescript_file_with_complexity(path)
                    .await
                    .ok()
            }
            #[cfg(not(feature = "typescript-ast"))]
            None
        }
        "python-uv" => {
            #[cfg(feature = "python-ast")]
            {
                use crate::services::ast_python;
                ast_python::analyze_python_file_with_complexity(path, None)
                    .await
                    .ok()
            }
            #[cfg(not(feature = "python-ast"))]
            None
        }
        _ => None,
    }
}

fn format_complexity_output(
    report: &crate::services::complexity::ComplexityReport,
    args: &AnalyzeComplexityArgs,
) -> String {
    use crate::services::complexity::{
        format_as_sarif, format_complexity_report, format_complexity_summary,
    };

    let format = args.format.as_deref().unwrap_or("summary");
    match format {
        "full" => format_complexity_report(report),
        "json" => serde_json::to_string_pretty(report).unwrap_or_default(),
        "sarif" => match format_as_sarif(report) {
            Ok(sarif) => sarif,
            Err(_) => "Error generating SARIF format".to_string(),
        },
        _ => format_complexity_summary(report), // default to summary
    }
}

fn format_complexity_rankings(
    rankings: &[(String, crate::services::ranking::CompositeComplexityScore)],
    args: &AnalyzeComplexityArgs,
) -> String {
    use crate::services::ranking::{ComplexityRanker, FileRanker};

    let format = args.format.as_deref().unwrap_or("summary");
    if format == "json" {
        let ranker = ComplexityRanker::default();
        let rankings_json = serde_json::json!({
            "analysis_type": ranker.ranking_type(),
            "timestamp": chrono::Utc::now().to_rfc3339(),
            "top_files": {
                "requested": rankings.len(),
                "returned": rankings.len()
            },
            "rankings": rankings.iter().enumerate().map(|(i, (file, score))| {
                serde_json::json!({
                    "rank": i + 1,
                    "file": file,
                    "metrics": {
                        "functions": score.function_count,
                        "max_cyclomatic": score.cyclomatic_max,
                        "avg_cognitive": score.cognitive_avg,
                        "halstead_effort": score.halstead_effort,
                        "total_score": score.total_score
                    }
                })
            }).collect::<Vec<_>>()
        });
        serde_json::to_string_pretty(&rankings_json).unwrap_or_default()
    } else {
        // Table format (default)
        let mut output = String::with_capacity(1024);
        output.push_str(&format!("## Top {} Complexity Files\n\n", rankings.len()));
        output.push_str("| Rank | File                               | Functions | Max Cyclomatic | Avg Cognitive | Halstead | Score |\n");
        output.push_str("|------|------------------------------------|-----------|--------------  |---------------|----------|-------|\n");

        for (i, (file, score)) in rankings.iter().enumerate() {
            output.push_str(&format!(
                "| {:>4} | {:<50} | {:>9} | {:>14} | {:>13.1} | {:>11.1} | {:>11.1} |\n",
                i + 1,
                file,
                score.function_count,
                score.cyclomatic_max,
                score.cognitive_avg,
                score.halstead_effort,
                score.total_score
            ));
        }
        output.push('\n');
        output
    }
}

#[cfg(test)]
mod extended_tools_complexity_tests {
    //! Covers pure helpers in extended_tools_complexity.rs (392 lines, 0
    //! prior tests). Skips async fns + MCP response builders that require
    //! a server fixture.
    use super::*;

    fn args(format: Option<&str>, top_files: Option<usize>) -> AnalyzeComplexityArgs {
        AnalyzeComplexityArgs {
            project_path: Some(".".to_string()),
            toolchain: None,
            format: format.map(String::from),
            max_cyclomatic: None,
            max_cognitive: None,
            include: None,
            top_files,
        }
    }

    // ── parse_complexity_args ──

    #[test]
    fn test_parse_complexity_args_minimal() {
        let json = serde_json::json!({"project_path": "."});
        let parsed = parse_complexity_args(json).unwrap();
        assert_eq!(parsed.project_path, Some(".".to_string()));
        assert_eq!(parsed.toolchain, None);
    }

    #[test]
    fn test_parse_complexity_args_full() {
        let json = serde_json::json!({
            "project_path": "src",
            "toolchain": "rust",
            "format": "json",
            "max_cyclomatic": 20,
            "max_cognitive": 30,
            "include": ["src/**", "lib/**"],
            "top_files": 10
        });
        let parsed = parse_complexity_args(json).unwrap();
        assert_eq!(parsed.toolchain, Some("rust".to_string()));
        assert_eq!(parsed.max_cyclomatic, Some(20));
        assert_eq!(parsed.top_files, Some(10));
    }

    #[test]
    fn test_parse_complexity_args_invalid_json_returns_err() {
        let json = serde_json::json!({"max_cyclomatic": "not-a-number"});
        let result = parse_complexity_args(json);
        assert!(result.is_err());
    }

    // ── detect_toolchain ──

    #[test]
    fn test_detect_toolchain_explicit_arg_wins() {
        let path = Path::new("/nonexistent");
        let toolchain = detect_toolchain(&Some("python-uv".to_string()), path);
        assert_eq!(toolchain, "python-uv");
    }

    #[test]
    fn test_detect_toolchain_falls_back_to_rust_on_unknown_dir() {
        // /tmp typically has no Cargo.toml/package.json/pyproject.toml.
        let path = Path::new("/tmp/__nonexistent_dir_xyz_test_123__");
        let toolchain = detect_toolchain(&None, path);
        // Default fallback is "rust".
        assert_eq!(toolchain, "rust");
    }

    #[test]
    fn test_detect_toolchain_uses_cwd_for_rust_detection() {
        // The project itself has Cargo.toml.
        let path = Path::new(".");
        let toolchain = detect_toolchain(&None, path);
        assert_eq!(toolchain, "rust");
    }

    // ── build_complexity_thresholds ──

    #[test]
    fn test_build_complexity_thresholds_default_when_no_overrides() {
        let a = args(None, None);
        let t = build_complexity_thresholds(&a);
        // Default thresholds are non-zero.
        assert!(t.cyclomatic_error > 0);
        assert!(t.cognitive_error > 0);
    }

    #[test]
    fn test_build_complexity_thresholds_overrides_set_warn_to_three_quarters() {
        let mut a = args(None, None);
        a.max_cyclomatic = Some(20);
        a.max_cognitive = Some(40);
        let t = build_complexity_thresholds(&a);
        assert_eq!(t.cyclomatic_error, 20);
        assert_eq!(t.cyclomatic_warn, 15); // 20 * 3 / 4
        assert_eq!(t.cognitive_error, 40);
        assert_eq!(t.cognitive_warn, 30); // 40 * 3 / 4
    }

    #[test]
    fn test_build_complexity_thresholds_min_one_warn_when_max_is_one() {
        let mut a = args(None, None);
        a.max_cyclomatic = Some(1);
        let t = build_complexity_thresholds(&a);
        // 1 * 3 / 4 = 0; .max(1) clamp ensures warn >= 1.
        assert_eq!(t.cyclomatic_warn, 1);
    }

    // ── should_analyze_file ──

    #[test]
    fn test_should_analyze_file_rust_arm() {
        assert!(should_analyze_file(Path::new("src/foo.rs"), "rust"));
        assert!(!should_analyze_file(Path::new("src/foo.py"), "rust"));
    }

    #[test]
    fn test_should_analyze_file_deno_arm_accepts_ts_tsx_js_jsx() {
        assert!(should_analyze_file(Path::new("a.ts"), "deno"));
        assert!(should_analyze_file(Path::new("a.tsx"), "deno"));
        assert!(should_analyze_file(Path::new("a.js"), "deno"));
        assert!(should_analyze_file(Path::new("a.jsx"), "deno"));
        assert!(!should_analyze_file(Path::new("a.rs"), "deno"));
    }

    #[test]
    fn test_should_analyze_file_python_arm() {
        assert!(should_analyze_file(Path::new("a.py"), "python-uv"));
        assert!(!should_analyze_file(Path::new("a.rs"), "python-uv"));
    }

    #[test]
    fn test_should_analyze_file_unknown_toolchain_returns_false() {
        assert!(!should_analyze_file(Path::new("a.rs"), "unknown"));
    }

    // ── matches_include_filters ──

    #[test]
    fn test_matches_include_filters_none_includes_all() {
        assert!(matches_include_filters(Path::new("any/file.rs"), &None));
    }

    #[test]
    fn test_matches_include_filters_empty_vec_includes_all() {
        assert!(matches_include_filters(
            Path::new("any/file.rs"),
            &Some(vec![])
        ));
    }

    #[test]
    fn test_matches_include_filters_any_match_passes() {
        let patterns = Some(vec!["**/never".to_string(), "*.rs".to_string()]);
        assert!(matches_include_filters(
            Path::new("src/foo.rs"),
            &patterns
        ));
    }

    #[test]
    fn test_matches_include_filters_no_match_filters_out() {
        let patterns = Some(vec!["*.py".to_string()]);
        assert!(!matches_include_filters(
            Path::new("src/foo.rs"),
            &patterns
        ));
    }

    // ── matches_pattern ──

    #[test]
    fn test_matches_pattern_double_star_with_suffix() {
        // `src/**` → split into ["src/", ""], len=2, suffix is "" → contains("").
        // `**/foo` → split into ["", "/foo"], len=2, suffix "/foo" trimmed → "foo".
        assert!(matches_pattern("a/foo/b.rs", "**/foo"));
        assert!(!matches_pattern("a/bar/b.rs", "**/foo"));
    }

    #[test]
    fn test_matches_pattern_extension_glob() {
        assert!(matches_pattern("src/foo.rs", "*.rs"));
        assert!(!matches_pattern("src/foo.py", "*.rs"));
    }

    #[test]
    fn test_matches_pattern_substring_match() {
        assert!(matches_pattern("src/handlers/foo.rs", "handlers"));
        assert!(!matches_pattern("src/lib/foo.rs", "handlers"));
    }

    #[test]
    fn test_matches_pattern_three_double_stars_returns_false() {
        // pattern has more than one "**" → split.len() != 2 → false.
        assert!(!matches_pattern("any/path", "**/foo/**"));
    }

    // ── resolve_project_path_complexity ──

    #[test]
    fn test_resolve_project_path_complexity_rejects_none() {
        let r = resolve_project_path_complexity(None);
        assert!(r.is_err());
    }

    #[test]
    fn test_resolve_project_path_complexity_rejects_empty_string() {
        let r = resolve_project_path_complexity(Some(String::new()));
        assert!(r.is_err());
    }

    #[test]
    fn test_resolve_project_path_complexity_accepts_concrete_path() {
        let r = resolve_project_path_complexity(Some(".".to_string()));
        assert!(r.is_ok());
    }
}