pmat 3.16.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
#![cfg_attr(coverage_nightly, coverage(off))]
//! Category B: Reproducibility Infrastructure (25 points)
//!
//! Independent verification requires exact environment replication.
//!
//! ## Sub-categories
//!
//! | ID | Name | Points | Description |
//! |----|------|--------|-------------|
//! | B1 | Artifact Availability | 10 | Locked dependencies, DOI/archival |
//! | B2 | Environment Reproducibility | 8 | Nix/Guix (bonus) or Docker |
//! | B3 | Result Reproduction | 7 | Single-command full reproduction |
//!
//! ## Academic Foundation
//!
//! - ACM Artifact Evaluation Badging (2020) [3]
//! - Wilkinson et al. (2016): FAIR Principles [5]
//! - Boettiger (2015): Docker for Reproducibility [8]

use crate::services::popper_score::models::{PopperCategoryScore, PopperFinding, PopperSubScore};
use crate::services::popper_score::scorer::{PopperScorer, PopperScorerResult};
use std::path::Path;

/// Scorer for Category B: Reproducibility Infrastructure (25 points)
pub struct ReproducibilityScorer;

impl ReproducibilityScorer {
    /// Create a new reproducibility scorer
    #[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
    pub fn new() -> Self {
        Self
    }

    /// B1: Artifact Availability (10 points)
    ///
    /// Checks for:
    /// - Lock files for dependencies (5 points)
    /// - DOI or archival reference (3 points)
    /// - Release artifacts (2 points)
    fn score_artifact_availability(&self, project_path: &Path) -> PopperSubScore {
        let mut earned: f64 = 0.0;
        let max: f64 = 10.0;
        let mut description = Vec::new();

        // Check for lock files (5 points)
        let lock_files = [
            ("Cargo.lock", 5.0, "Cargo.lock found"),
            ("package-lock.json", 5.0, "package-lock.json found"),
            ("yarn.lock", 5.0, "yarn.lock found"),
            ("pnpm-lock.yaml", 5.0, "pnpm-lock.yaml found"),
            ("poetry.lock", 5.0, "poetry.lock found"),
            ("Pipfile.lock", 5.0, "Pipfile.lock found"),
            ("Gemfile.lock", 5.0, "Gemfile.lock found"),
            ("go.sum", 5.0, "go.sum found"),
            ("flake.lock", 5.0, "flake.lock found"),
            ("uv.lock", 5.0, "uv.lock found"),
        ];

        let mut lock_found = false;
        for (file, points, desc) in lock_files {
            if project_path.join(file).exists() {
                earned += points;
                description.push(desc);
                lock_found = true;
                break;
            }
        }

        // Check for DOI or archival (3 points)
        let readme_path = project_path.join("README.md");
        if readme_path.exists() {
            if let Ok(content) = std::fs::read_to_string(&readme_path) {
                let content_lower = content.to_lowercase();
                if content.contains("10.") && content.contains("/")
                    || content_lower.contains("doi")
                    || content_lower.contains("zenodo")
                    || content_lower.contains("figshare")
                    || content_lower.contains("osf.io")
                {
                    earned += 3.0;
                    description.push("DOI/archival reference found");
                }
            }
        }

        // Check CITATION.cff (2 points)
        if project_path.join("CITATION.cff").exists() {
            earned += 2.0;
            description.push("CITATION.cff found");
        } else if project_path.join("CITATION").exists() {
            earned += 1.0;
            description.push("CITATION file found");
        }

        if !lock_found && description.is_empty() {
            description.push("no lock files or archival references");
        }

        PopperSubScore::new(
            "B1",
            "Artifact Availability",
            earned.min(max),
            max,
            &description.join(", "),
        )
    }

    /// B2: Environment Reproducibility (8 points)
    ///
    /// Checks for:
    /// - Nix/Guix flake (5 points - BONUS over Docker)
    /// - Docker/Containerfile (3 points)
    /// - Dev container config (2 points)
    fn score_environment_reproducibility(&self, project_path: &Path) -> PopperSubScore {
        let mut earned: f64 = 0.0;
        let max: f64 = 8.0;
        let mut description: Vec<String> = Vec::new();

        // Nix/Guix hermetic builds (5 points - preferred per Heijunka)
        let nix_files = ["flake.nix", "shell.nix", "default.nix"];
        for nix_file in nix_files {
            if project_path.join(nix_file).exists() {
                earned += 5.0;
                description.push(format!("{} found (hermetic)", nix_file));
                break;
            }
        }

        // Guix (5 points)
        if project_path.join("guix.scm").exists() {
            earned = earned.max(5.0);
            description.push("guix.scm found (hermetic)".to_string());
        }

        // Docker (3 points - less preferred)
        if earned < 3.0 {
            let docker_files = ["Dockerfile", "docker-compose.yml", "docker-compose.yaml"];
            for docker_file in docker_files {
                if project_path.join(docker_file).exists() {
                    earned = earned.max(3.0);
                    description.push(format!("{} found", docker_file));
                    break;
                }
            }

            // Containerfile (3 points)
            if project_path.join("Containerfile").exists() {
                earned = earned.max(3.0);
                description.push("Containerfile found".to_string());
            }
        }

        // Dev container (2 points)
        let devcontainer_paths = [".devcontainer/devcontainer.json", ".devcontainer.json"];
        for devcontainer in devcontainer_paths {
            if project_path.join(devcontainer).exists() {
                earned = (earned + 2.0).min(max);
                description.push("devcontainer config found".to_string());
                break;
            }
        }

        if description.is_empty() {
            description.push("no environment reproducibility config".to_string());
        }

        PopperSubScore::new(
            "B2",
            "Environment Reproducibility",
            earned.min(max),
            max,
            &description.join(", "),
        )
    }

    /// B3: Result Reproduction (7 points)
    ///
    /// Checks for:
    /// - Single-command build (2 points)
    /// - Single-command test (2 points)
    /// - Makefile/justfile with documented targets (2 points)
    /// - Documented reproduction steps (1 point)
    fn score_result_reproduction(&self, project_path: &Path) -> PopperSubScore {
        let mut earned: f64 = 0.0;
        let max: f64 = 7.0;
        let mut description = Vec::new();

        // Check for Makefile/justfile (2 points)
        if project_path.join("Makefile").exists() {
            earned += 2.0;
            description.push("Makefile found");
        } else if project_path.join("justfile").exists() {
            earned += 2.0;
            description.push("justfile found");
        }

        // Check for standard build commands (2 points)
        let cargo_toml = project_path.join("Cargo.toml");
        let package_json = project_path.join("package.json");
        let pyproject = project_path.join("pyproject.toml");

        if cargo_toml.exists() || package_json.exists() || pyproject.exists() {
            earned += 2.0;
            description.push("standard build config found");
        }

        // Check README for reproduction steps (2 points)
        let readme_path = project_path.join("README.md");
        if readme_path.exists() {
            if let Ok(content) = std::fs::read_to_string(&readme_path) {
                let content_lower = content.to_lowercase();

                // Check for installation/build instructions
                if content_lower.contains("## install")
                    || content_lower.contains("## build")
                    || content_lower.contains("## getting started")
                    || content_lower.contains("```bash")
                    || content_lower.contains("```shell")
                {
                    earned += 2.0;
                    description.push("installation instructions found");
                }

                // Check for reproduction/usage instructions (1 point)
                if content_lower.contains("## usage")
                    || content_lower.contains("## reproduce")
                    || content_lower.contains("## quickstart")
                {
                    earned += 1.0;
                    description.push("usage documentation found");
                }
            }
        }

        if description.is_empty() {
            description.push("no reproduction infrastructure");
        }

        PopperSubScore::new(
            "B3",
            "Result Reproduction",
            earned.min(max),
            max,
            &description.join(", "),
        )
    }
}

impl Default for ReproducibilityScorer {
    fn default() -> Self {
        Self::new()
    }
}

impl PopperScorer for ReproducibilityScorer {
    fn name(&self) -> &str {
        "Reproducibility Infrastructure"
    }

    fn category_id(&self) -> char {
        'B'
    }

    fn max_points(&self) -> f64 {
        25.0
    }

    fn score(&self, project_path: &Path) -> PopperScorerResult<PopperCategoryScore> {
        let mut category = PopperCategoryScore::new(self.name(), 0.0, self.max_points());

        // Score each sub-category
        let b1 = self.score_artifact_availability(project_path);
        let b2 = self.score_environment_reproducibility(project_path);
        let b3 = self.score_result_reproduction(project_path);

        // Add findings based on scores
        if b1.earned < 5.0 {
            category.add_finding(PopperFinding::warning(
                "Artifact availability needs improvement - add lock files and archival references",
                10.0 - b1.earned,
            ));
        } else {
            category.add_finding(PopperFinding::positive("Good artifact availability"));
        }

        if b2.earned < 3.0 {
            category.add_finding(PopperFinding::warning(
                "Consider adding Nix/Docker for environment reproducibility",
                8.0 - b2.earned,
            ));
        } else if b2.earned >= 5.0 {
            category.add_finding(PopperFinding::positive(
                "Excellent hermetic builds with Nix/Guix",
            ));
        }

        if b3.earned < 4.0 {
            category.add_finding(PopperFinding::warning(
                "Result reproduction could be easier - document build steps",
                7.0 - b3.earned,
            ));
        }

        // Add sub-scores
        category.add_sub_score(b1);
        category.add_sub_score(b2);
        category.add_sub_score(b3);

        Ok(category)
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use tempfile::tempdir;

    #[test]
    fn test_reproducibility_scorer_basics() {
        let scorer = ReproducibilityScorer::new();
        assert_eq!(scorer.name(), "Reproducibility Infrastructure");
        assert_eq!(scorer.category_id(), 'B');
        assert_eq!(scorer.max_points(), 25.0);
        assert!(!scorer.is_gateway());
    }

    #[test]
    fn test_project_with_cargo_lock() {
        let temp_dir = tempdir().unwrap();

        // Create Cargo.lock
        fs::write(temp_dir.path().join("Cargo.lock"), "# Lock file").unwrap();

        let scorer = ReproducibilityScorer::new();
        let result = scorer.score(temp_dir.path()).unwrap();

        // Should have earned points for lock file
        let b1 = result.sub_scores.iter().find(|s| s.id == "B1").unwrap();
        assert!(b1.earned >= 5.0);
    }

    #[test]
    fn test_project_with_nix_flake() {
        let temp_dir = tempdir().unwrap();

        // Create flake.nix
        fs::write(temp_dir.path().join("flake.nix"), "{ outputs = ... }").unwrap();

        let scorer = ReproducibilityScorer::new();
        let result = scorer.score(temp_dir.path()).unwrap();

        // Should have earned bonus points for Nix
        let b2 = result.sub_scores.iter().find(|s| s.id == "B2").unwrap();
        assert!(b2.earned >= 5.0);
    }

    #[test]
    fn test_project_with_dockerfile() {
        let temp_dir = tempdir().unwrap();

        // Create Dockerfile
        fs::write(temp_dir.path().join("Dockerfile"), "FROM rust:1.70").unwrap();

        let scorer = ReproducibilityScorer::new();
        let result = scorer.score(temp_dir.path()).unwrap();

        // Should have earned points for Docker (less than Nix)
        let b2 = result.sub_scores.iter().find(|s| s.id == "B2").unwrap();
        assert!(b2.earned >= 3.0);
        assert!(b2.earned < 5.0);
    }

    #[test]
    fn test_project_with_makefile() {
        let temp_dir = tempdir().unwrap();

        // Create Makefile
        fs::write(
            temp_dir.path().join("Makefile"),
            "build:\n\tcargo build\ntest:\n\tcargo test",
        )
        .unwrap();

        let scorer = ReproducibilityScorer::new();
        let result = scorer.score(temp_dir.path()).unwrap();

        // Should have earned points for Makefile
        let b3 = result.sub_scores.iter().find(|s| s.id == "B3").unwrap();
        assert!(b3.earned >= 2.0);
    }

    #[test]
    fn test_full_reproducibility_setup() {
        let temp_dir = tempdir().unwrap();

        // Create complete reproducibility setup
        fs::write(temp_dir.path().join("Cargo.lock"), "# Lock").unwrap();
        fs::write(temp_dir.path().join("Cargo.toml"), "[package]").unwrap();
        fs::write(temp_dir.path().join("flake.nix"), "{ }").unwrap();
        fs::write(temp_dir.path().join("Makefile"), "build:\n\t").unwrap();
        fs::write(
            temp_dir.path().join("README.md"),
            "# Project\n## Installation\n```bash\nmake build\n```\n## Usage\nRun it",
        )
        .unwrap();
        fs::write(temp_dir.path().join("CITATION.cff"), "cff-version: 1.2.0").unwrap();

        let scorer = ReproducibilityScorer::new();
        let result = scorer.score(temp_dir.path()).unwrap();

        // Should have high score
        assert!(result.earned > 15.0);
    }
}