pmat 3.30.1

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP)
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
impl Default for RefactorConfig {
    fn default() -> Self {
        Self {
            target_complexity: 20,
            remove_satd: true,
            max_function_lines: 50,
            thresholds: Thresholds::default(),
            strategies: RefactorStrategies::default(),
            parallel_workers: 4,
            memory_limit_mb: 512,
            batch_size: 10,
            priority_expression: None,
            auto_commit_template: None,
        }
    }
}

impl Default for Thresholds {
    fn default() -> Self {
        Self {
            cyclomatic_warn: 10,
            cyclomatic_error: 20,
            cognitive_warn: 15,
            cognitive_error: 30,
            tdg_warn: 1.5,
            tdg_error: 2.0,
        }
    }
}

impl Default for RefactorStrategies {
    fn default() -> Self {
        Self {
            prefer_functional: true,
            use_early_returns: true,
            extract_helpers: true,
        }
    }
}

impl RefactorStateMachine {
    #[must_use]
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "path_exists")]
    /// Create a new instance.
    pub fn new(targets: Vec<PathBuf>, config: RefactorConfig) -> Self {
        let initial_state = if targets.is_empty() {
            State::Complete {
                summary: Summary::default(),
            }
        } else {
            State::Scan {
                targets: targets.clone(),
            }
        };

        Self {
            current: initial_state,
            history: Vec::new(),
            config,
            targets,
            current_target_index: 0,
        }
    }

    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// Advance.
    pub fn advance(&mut self) -> Result<&State, String> {
        let next = match &self.current {
            State::Scan { targets } => {
                if targets.is_empty() {
                    State::Complete {
                        summary: self.session_summary(),
                    }
                } else {
                    State::Analyze {
                        current: Self::file_id(&targets[0]),
                    }
                }
            }
            State::Analyze { current } => State::Plan {
                violations: self.find_violations(current),
            },
            State::Plan { violations } => {
                if violations.is_empty() {
                    let summary = self.session_summary();
                    self.next_target()
                        .map_or(State::Complete { summary }, |t| State::Analyze { current: t })
                } else {
                    State::Refactor {
                        operation: violations[0].suggested_fix.clone().unwrap_or(
                            RefactorOp::SimplifyExpression {
                                expr: "complex".to_string(),
                                simplified: "simple".to_string(),
                            },
                        ),
                    }
                }
            }
            State::Refactor { .. } => State::Test {
                command: "make test-fast".to_string(),
            },
            State::Test { .. } => State::Lint { strict: true },
            State::Lint { .. } => State::Emit {
                payload: self.compute_payload(),
            },
            State::Emit { .. } => State::Checkpoint {
                reason: "cycle_complete".to_string(),
            },
            State::Checkpoint { .. } => {
                let summary = self.session_summary();
                self.next_target()
                    .map_or(State::Complete { summary }, |t| State::Analyze { current: t })
            }
            State::Complete { .. } => {
                return Ok(&self.current);
            }
        };

        self.transition_to(next)
    }

    fn transition_to(&mut self, new_state: State) -> Result<&State, String> {
        let transition = StateTransition {
            from: self.current.clone(),
            to: new_state.clone(),
            timestamp: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("internal error")
                .as_secs(),
            metrics_before: MetricSet::default(),
            metrics_after: None,
            applied_refactor: None,
        };

        self.history.push(transition);
        self.current = new_state;
        Ok(&self.current)
    }

    fn find_violations(&self, file_id: &FileId) -> Vec<Violation> {
        // Check thresholds and create violations
        let mut violations = Vec::new();

        // Simulate finding a high complexity violation
        if file_id.path.to_string_lossy().contains("complex") {
            violations.push(Violation {
                violation_type: ViolationType::HighComplexity,
                location: Location {
                    file: file_id.path.clone(),
                    line: 100,
                    column: 1,
                },
                severity: Severity::High,
                description: "Function exceeds complexity threshold".to_string(),
                suggested_fix: Some(RefactorOp::ExtractFunction {
                    name: "extract_helper".to_string(),
                    start: BytePos {
                        byte: 1000,
                        line: 100,
                        column: 1,
                    },
                    end: BytePos {
                        byte: 2000,
                        line: 150,
                        column: 1,
                    },
                    params: vec!["param1".to_string()],
                }),
            });
        }

        violations
    }

    fn next_target(&mut self) -> Option<FileId> {
        self.current_target_index += 1;
        if self.current_target_index < self.targets.len() {
            Some(Self::file_id(&self.targets[self.current_target_index]))
        } else {
            None
        }
    }

    /// Identify a target by its *contents*.
    ///
    /// `FileId.hash` was the literal `0` with the comment "Will be computed
    /// during analysis" — nothing ever computed it, so `refactor.nextIteration`
    /// reported `{"path": "…", "hash": 0}` for every file, including files that
    /// did not exist. An unreadable file keeps hash 0: that is "not measured",
    /// not "empty".
    fn file_id(path: &std::path::Path) -> FileId {
        let hash = std::fs::read(path).map_or(0, |bytes| {
            let digest = blake3::hash(&bytes);
            let mut first8 = [0u8; 8];
            first8.copy_from_slice(&digest.as_bytes()[..8]);
            u64::from_le_bytes(first8)
        });
        FileId {
            path: path.to_path_buf(),
            hash,
        }
    }

    /// Summarise the session from what actually happened.
    ///
    /// Every `State::Complete` used to carry `Summary::default()` — a struct
    /// of hardcoded zeros — so a session that scanned and analysed a real file
    /// still reported `files_processed: 0` and `total_time: 0s`. The counts
    /// below are derived from the recorded transitions. `refactors_applied`,
    /// `complexity_reduction` and `satd_removed` stay zero because this state
    /// machine applies nothing (the tools disclose the simulated analysis
    /// engine in their MCP descriptions); reporting anything else would be
    /// inventing work that never happened.
    fn session_summary(&self) -> Summary {
        let files_processed = self
            .history
            .iter()
            .filter(|t| matches!(t.to, State::Analyze { .. }))
            .count() as u32;

        let total_time = self
            .history
            .first()
            .and_then(|first| {
                SystemTime::now()
                    .duration_since(UNIX_EPOCH)
                    .ok()
                    .map(|now| Duration::from_secs(now.as_secs().saturating_sub(first.timestamp)))
            })
            .unwrap_or_default();

        Summary {
            files_processed,
            refactors_applied: 0,
            complexity_reduction: 0.0,
            satd_removed: 0,
            total_time,
        }
    }

    fn compute_payload(&self) -> DefectPayload {
        DefectPayload {
            file_hash: 0,
            tdg_score: 1.0,
            complexity: (10, 15),
            dead_symbols: 0,
            timestamp: SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .expect("internal error")
                .as_secs(),
            severity_flags: 0,
            refactor_available: true,
            refactor_type: RefactorType::None,
            estimated_improvement: 0.5,
            _padding: [0; 2],
        }
    }
}

#[cfg(test)]
mod session_summary_tests {
    //! The Complete summary must describe the session that ran, not a
    //! `Summary::default()` of hardcoded zeros.
    //!
    //! #975: `completing_a_one_file_session_reports_that_file` used
    //! `let State::X { .. } = .. else { panic!(..) }`. Those `else` arms only
    //! execute when the test *fails*, so four of its lines were permanently
    //! uncovered and no passing run could ever close the gap. The state is
    //! projected through the two helpers below instead, and a dedicated test
    //! exercises their rejecting arm — so every line here is reachable by a
    //! green run.
    use super::*;

    /// `Some` iff the machine is currently analysing a file.
    fn analysed_file(state: &State) -> Option<&FileId> {
        match state {
            State::Analyze { current } => Some(current),
            _ => None,
        }
    }

    /// `Some` iff the session has finished.
    fn finished_summary(state: &State) -> Option<&Summary> {
        match state {
            State::Complete { summary } => Some(summary),
            _ => None,
        }
    }

    /// Advance until the session completes, or give up after `budget` steps.
    fn run_to_completion(sm: &mut RefactorStateMachine, budget: usize) -> Summary {
        for _ in 0..budget {
            if let Some(summary) = finished_summary(&sm.current) {
                return summary.clone();
            }
            sm.advance().expect("advance must not fail");
        }
        panic!("session did not complete within {budget} transitions");
    }

    #[test]
    fn the_state_projections_reject_states_they_do_not_describe() {
        // A fresh multi-target machine sits in Scan, which is neither Analyze
        // nor Complete. This covers the rejecting arm of both helpers.
        let sm =
            RefactorStateMachine::new(vec![PathBuf::from("src/lib.rs")], RefactorConfig::default());
        assert!(matches!(sm.current, State::Scan { .. }));
        assert!(analysed_file(&sm.current).is_none());
        assert!(finished_summary(&sm.current).is_none());
    }

    #[test]
    fn completing_a_one_file_session_reports_that_file() {
        let dir = tempfile::tempdir().expect("tempdir");
        let file = dir.path().join("lib.rs");
        std::fs::write(&file, "pub fn f() -> i32 { 1 }\n").expect("write fixture");

        let mut sm = RefactorStateMachine::new(vec![file.clone()], RefactorConfig::default());
        // Scan -> Analyze
        sm.advance().expect("scan");
        let current = analysed_file(&sm.current).expect("Scan must advance to Analyze");
        assert_eq!(
            current.path, file,
            "the analysed target must be the file we handed in"
        );
        assert_ne!(
            current.hash, 0,
            "the analysed file's id must be a real content hash"
        );

        sm.advance().expect("analyze"); // -> Plan (no violations)
        sm.advance().expect("plan"); // -> Complete
        let summary = finished_summary(&sm.current).expect("an empty plan must complete");
        assert_eq!(
            summary.files_processed, 1,
            "one file was scanned and analysed"
        );
    }

    #[test]
    fn a_two_file_session_reports_both_files() {
        // Guards against `files_processed` being pinned to 0 or 1: the count
        // has to track how many files the run actually reached Analyze on.
        let dir = tempfile::tempdir().expect("tempdir");
        let a = dir.path().join("a.rs");
        let b = dir.path().join("b.rs");
        std::fs::write(&a, "pub fn a() {}\n").expect("write a");
        std::fs::write(&b, "pub fn b() {}\n").expect("write b");

        let mut sm = RefactorStateMachine::new(vec![a, b], RefactorConfig::default());
        let summary = run_to_completion(&mut sm, 16);
        assert_eq!(
            summary.files_processed, 2,
            "both targets were analysed, so both must be counted"
        );
    }

    #[test]
    fn a_session_with_no_targets_reports_nothing_processed() {
        let mut sm = RefactorStateMachine::new(vec![], RefactorConfig::default());
        let summary = finished_summary(&sm.current)
            .expect("an empty target list completes immediately")
            .clone();
        assert_eq!(summary.files_processed, 0);
        assert_eq!(summary.total_time, Duration::from_secs(0));
        // And Complete is terminal: no further transition is recorded.
        sm.advance().expect("advance on Complete");
        assert!(sm.history.is_empty());
    }

    #[test]
    fn a_files_contents_decide_its_hash() {
        let dir = tempfile::tempdir().expect("tempdir");
        let a = dir.path().join("a.rs");
        let b = dir.path().join("b.rs");
        std::fs::write(&a, "pub fn a() {}\n").expect("write a");
        std::fs::write(&b, "pub fn b() {}\n").expect("write b");
        assert_ne!(
            RefactorStateMachine::file_id(&a).hash,
            RefactorStateMachine::file_id(&b).hash
        );
        // A path that cannot be read is "not measured", i.e. still 0.
        assert_eq!(
            RefactorStateMachine::file_id(&dir.path().join("missing.rs")).hash,
            0
        );
    }
}

impl Default for MetricSet {
    fn default() -> Self {
        Self {
            complexity: (0, 0),
            tdg_score: 0.0,
            dead_code: Vec::new(),
            satd_count: 0,
            provability: 0.0,
        }
    }
}

impl Default for Summary {
    fn default() -> Self {
        Self {
            files_processed: 0,
            refactors_applied: 0,
            complexity_reduction: 0.0,
            satd_removed: 0,
            total_time: Duration::from_secs(0),
        }
    }
}

impl Violation {
    #[must_use]
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    /// To op.
    pub fn to_op(&self) -> RefactorOp {
        self.suggested_fix
            .clone()
            .unwrap_or_else(|| match self.violation_type {
                ViolationType::HighComplexity => RefactorOp::ExtractFunction {
                    name: "extracted_function".to_string(),
                    start: BytePos {
                        byte: 0,
                        line: self.location.line,
                        column: self.location.column,
                    },
                    end: BytePos {
                        byte: 100,
                        line: self.location.line + 10,
                        column: 0,
                    },
                    params: vec![],
                },
                ViolationType::DeepNesting => RefactorOp::FlattenNesting {
                    function: "function_name".to_string(),
                    strategy: NestingStrategy::EarlyReturn,
                },
                ViolationType::SelfAdmittedTechDebt => RefactorOp::RemoveSatd {
                    location: self.location.clone(),
                    fix: SatdFix::Remove,
                },
                _ => RefactorOp::SimplifyExpression {
                    expr: "complex".to_string(),
                    simplified: "simple".to_string(),
                },
            })
    }
}