pmat 3.15.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
#![cfg_attr(coverage_nightly, coverage(off))]
//! Property-based tests for state machine verification
//! 
//! This module verifies that the refactor auto state machine maintains
//! critical invariants across all possible transitions.

use proptest::prelude::*;
use std::collections::HashMap;
use std::path::PathBuf;

/// Quality metrics for a file
#[derive(Debug, Clone)]
pub struct QualityMetrics {
    pub complexity: u32,
    pub technical_debt: u32,
    pub test_coverage: f64,
    pub documentation_score: f64,
}

/// File quality score
#[derive(Debug, Clone)]
pub struct FileQualityScore {
    pub path: PathBuf,
    pub metrics: QualityMetrics,
    pub priority: f64,
}

/// Refactor action that can be applied
#[derive(Debug, Clone)]
pub enum RefactorAction {
    ExtractFunction { file: PathBuf, lines: (usize, usize) },
    SimplifyCondition { file: PathBuf, line: usize },
    RemoveDeadCode { file: PathBuf, lines: Vec<usize> },
    ImproveNaming { file: PathBuf, old_name: String, new_name: String },
    AddDocumentation { file: PathBuf, item: String },
}

/// State of the refactoring process
#[derive(Debug, Clone)]
pub struct RefactorState {
    pub iteration: u32,
    pub current_quality: QualityMetrics,
    pub file_scores: HashMap<PathBuf, FileQualityScore>,
    pub completed_actions: Vec<RefactorAction>,
    pub pending_files: Vec<PathBuf>,
}

impl RefactorState {
    /// Create a new refactor state
    pub fn new() -> Self {
        Self {
            iteration: 0,
            current_quality: QualityMetrics {
                complexity: 100,
                technical_debt: 50,
                test_coverage: 0.6,
                documentation_score: 0.5,
            },
            file_scores: HashMap::new(),
            completed_actions: Vec::new(),
            pending_files: Vec::new(),
        }
    }
    
    /// Select the next file to refactor (deterministic)
    pub fn select_next_file(&self) -> Option<PathBuf> {
        // Sort files by priority for deterministic selection
        let mut files: Vec<_> = self.file_scores.iter().collect();
        files.sort_by(|a, b| {
            b.1.priority.partial_cmp(&a.1.priority)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        
        files.first().map(|(path, _)| (*path).clone())
    }
    
    /// Get the next refactoring action
    pub fn next_action(&self) -> Option<RefactorAction> {
        self.select_next_file().map(|file| {
            // Deterministically choose action based on current metrics
            if self.current_quality.complexity > 50 {
                RefactorAction::ExtractFunction {
                    file: file.clone(),
                    lines: (10, 20),
                }
            } else if self.current_quality.technical_debt > 30 {
                RefactorAction::SimplifyCondition {
                    file: file.clone(),
                    line: 15,
                }
            } else {
                RefactorAction::RemoveDeadCode {
                    file,
                    lines: vec![25, 30],
                }
            }
        })
    }
    
    /// Apply an action and return the new state
    pub fn apply_action(&self, action: RefactorAction) -> Self {
        let mut new_state = self.clone();
        new_state.iteration += 1;
        new_state.completed_actions.push(action.clone());
        
        // Update quality metrics based on action
        match action {
            RefactorAction::ExtractFunction { .. } => {
                new_state.current_quality.complexity = 
                    new_state.current_quality.complexity.saturating_sub(5);
            }
            RefactorAction::SimplifyCondition { .. } => {
                new_state.current_quality.complexity = 
                    new_state.current_quality.complexity.saturating_sub(2);
                new_state.current_quality.technical_debt = 
                    new_state.current_quality.technical_debt.saturating_sub(3);
            }
            RefactorAction::RemoveDeadCode { .. } => {
                new_state.current_quality.technical_debt = 
                    new_state.current_quality.technical_debt.saturating_sub(5);
            }
            RefactorAction::ImproveNaming { .. } => {
                new_state.current_quality.technical_debt = 
                    new_state.current_quality.technical_debt.saturating_sub(1);
            }
            RefactorAction::AddDocumentation { .. } => {
                new_state.current_quality.documentation_score = 
                    (new_state.current_quality.documentation_score + 0.1).min(1.0);
            }
        }
        
        new_state
    }
    
    /// Compute overall progress (0.0 to 1.0)
    pub fn compute_progress(&self) -> f64 {
        let complexity_progress = 1.0 - (self.current_quality.complexity as f64 / 100.0);
        let debt_progress = 1.0 - (self.current_quality.technical_debt as f64 / 50.0);
        let coverage_progress = self.current_quality.test_coverage;
        let doc_progress = self.current_quality.documentation_score;
        
        (complexity_progress + debt_progress + coverage_progress + doc_progress) / 4.0
    }
    
    /// Check if refactoring is complete
    pub fn is_complete(&self) -> bool {
        self.current_quality.complexity <= 20 &&
        self.current_quality.technical_debt == 0 &&
        self.current_quality.test_coverage >= 0.8 &&
        self.current_quality.documentation_score >= 0.8
    }
}

// Arbitrary implementations for property testing
impl Arbitrary for QualityMetrics {
    type Parameters = ();
    type Strategy = BoxedStrategy<Self>;
    
    fn arbitrary_with(_: Self::Parameters) -> Self::Strategy {
        (
            0u32..200,  // complexity
            0u32..100,  // technical_debt
            0.0f64..1.0,  // test_coverage
            0.0f64..1.0,  // documentation_score
        ).prop_map(|(complexity, technical_debt, test_coverage, documentation_score)| {
            QualityMetrics {
                complexity,
                technical_debt,
                test_coverage,
                documentation_score,
            }
        }).boxed()
    }
}

impl Arbitrary for FileQualityScore {
    type Parameters = ();
    type Strategy = BoxedStrategy<Self>;
    
    fn arbitrary_with(_: Self::Parameters) -> Self::Strategy {
        (
            "[a-z0-9_/]+\\.rs",
            any::<QualityMetrics>(),
            0.0f64..100.0,
        ).prop_map(|(path_str, metrics, priority)| {
            FileQualityScore {
                path: PathBuf::from(path_str),
                metrics,
                priority,
            }
        }).boxed()
    }
}

impl Arbitrary for RefactorAction {
    type Parameters = ();
    type Strategy = BoxedStrategy<Self>;
    
    fn arbitrary_with(_: Self::Parameters) -> Self::Strategy {
        prop_oneof![
            ("[a-z0-9_/]+\\.rs", 1usize..100, 1usize..100).prop_map(|(path, start, len)| {
                RefactorAction::ExtractFunction {
                    file: PathBuf::from(path),
                    lines: (start, start + len),
                }
            }),
            ("[a-z0-9_/]+\\.rs", 1usize..500).prop_map(|(path, line)| {
                RefactorAction::SimplifyCondition {
                    file: PathBuf::from(path),
                    line,
                }
            }),
            ("[a-z0-9_/]+\\.rs", prop::collection::vec(1usize..500, 1..10)).prop_map(|(path, lines)| {
                RefactorAction::RemoveDeadCode {
                    file: PathBuf::from(path),
                    lines,
                }
            }),
        ].boxed()
    }
}

/// Arbitrary state for property testing
#[derive(Debug, Clone)]
struct RefactorStateArb {
    iteration: u32,
    current_quality: QualityMetrics,
    file_scores: HashMap<PathBuf, FileQualityScore>,
    completed_actions: Vec<RefactorAction>,
}

impl From<RefactorStateArb> for RefactorState {
    fn from(arb: RefactorStateArb) -> Self {
        let mut state = RefactorState::new();
        state.iteration = arb.iteration;
        state.current_quality = arb.current_quality;
        state.file_scores = arb.file_scores;
        state.completed_actions = arb.completed_actions;
        state
    }
}

impl Arbitrary for RefactorStateArb {
    type Parameters = ();
    type Strategy = BoxedStrategy<Self>;
    
    fn arbitrary_with(_: Self::Parameters) -> Self::Strategy {
        (
            0u32..1000,
            any::<QualityMetrics>(),
            prop::collection::hash_map(
                "[a-z0-9_/]+\\.rs".prop_map(PathBuf::from),
                any::<FileQualityScore>(),
                0..10
            ),
            prop::collection::vec(any::<RefactorAction>(), 0..50),
        ).prop_map(|(iteration, current_quality, file_scores, completed_actions)| {
            RefactorStateArb {
                iteration,
                current_quality,
                file_scores,
                completed_actions,
            }
        }).boxed()
    }
}

proptest! {
    /// Property: File selection is deterministic
    #[test]
    fn state_machine_file_selection_deterministic(initial_state in any::<RefactorStateArb>()) {
        let state = RefactorState::from(initial_state);
        
        // Multiple calls should return the same file
        let file1 = state.select_next_file();
        let file2 = state.select_next_file();
        prop_assert_eq!(file1, file2, "File selection must be deterministic");
    }
    
    /// Property: Progress is monotonic
    #[test]
    fn state_machine_progress_monotonic(initial_state in any::<RefactorStateArb>()) {
        let state = RefactorState::from(initial_state);
        
        if let Some(action) = state.next_action() {
            let new_state = state.apply_action(action);
            prop_assert!(
                new_state.compute_progress() >= state.compute_progress(),
                "Progress must be monotonic: {} -> {}",
                state.compute_progress(),
                new_state.compute_progress()
            );
        }
    }
    
    /// Property: State machine terminates
    #[test]
    fn state_machine_termination_guarantee(initial_state in any::<RefactorStateArb>()) {
        let mut state = RefactorState::from(initial_state);
        let max_iterations = 1000;
        
        for i in 0..max_iterations {
            if state.is_complete() {
                break;
            }
            
            if let Some(action) = state.next_action() {
                state = state.apply_action(action);
            } else {
                // No more actions available
                break;
            }
            
            // Ensure we're making progress
            if i == max_iterations - 1 {
                prop_assert!(
                    state.is_complete() || state.iteration >= max_iterations as u32,
                    "State machine must terminate or reach max iterations"
                );
            }
        }
    }
    
    /// Property: Actions reduce technical debt
    #[test]
    fn actions_reduce_technical_debt(initial_state in any::<RefactorStateArb>()) {
        let state = RefactorState::from(initial_state);
        
        // Apply a series of actions
        let mut current = state.clone();
        for _ in 0..10 {
            if let Some(action) = current.next_action() {
                let new_state = current.apply_action(action);
                
                // Technical debt should not increase
                prop_assert!(
                    new_state.current_quality.technical_debt <= current.current_quality.technical_debt,
                    "Technical debt increased: {} -> {}",
                    current.current_quality.technical_debt,
                    new_state.current_quality.technical_debt
                );
                
                current = new_state;
            } else {
                break;
            }
        }
    }
    
    /// Property: Iteration count increases monotonically
    #[test]
    fn iteration_count_monotonic(initial_state in any::<RefactorStateArb>()) {
        let state = RefactorState::from(initial_state);
        
        if let Some(action) = state.next_action() {
            let new_state = state.apply_action(action);
            prop_assert!(
                new_state.iteration > state.iteration,
                "Iteration count must increase: {} -> {}",
                state.iteration,
                new_state.iteration
            );
        }
    }
    
    /// Property: Completed actions are tracked
    #[test]
    fn completed_actions_tracked(initial_state in any::<RefactorStateArb>()) {
        let state = RefactorState::from(initial_state);
        let initial_count = state.completed_actions.len();
        
        if let Some(action) = state.next_action() {
            let new_state = state.apply_action(action.clone());
            prop_assert_eq!(
                new_state.completed_actions.len(),
                initial_count + 1,
                "Action not tracked in completed list"
            );
            
            // The last action should be the one we applied
            prop_assert_eq!(
                new_state.completed_actions.last(),
                Some(&action),
                "Last action doesn't match applied action"
            );
        }
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
    use super::*;
    
    #[test]
    fn test_refactor_state_new() {
        let state = RefactorState::new();
        assert_eq!(state.iteration, 0);
        assert_eq!(state.current_quality.complexity, 100);
        assert_eq!(state.current_quality.technical_debt, 50);
        assert!(state.completed_actions.is_empty());
    }
    
    #[test]
    fn test_progress_calculation() {
        let mut state = RefactorState::new();
        let initial_progress = state.compute_progress();
        
        // Improve metrics
        state.current_quality.complexity = 20;
        state.current_quality.technical_debt = 0;
        state.current_quality.test_coverage = 0.9;
        state.current_quality.documentation_score = 0.9;
        
        let final_progress = state.compute_progress();
        assert!(final_progress > initial_progress);
        assert!(state.is_complete());
    }
}