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
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
#![cfg_attr(coverage_nightly, coverage(off))]
//! Stack-wide hook management for sovereign AI repos.
//!
//! Implements `pmat hooks install --stack` and `pmat hooks status --stack`
//! to manage pre-commit hooks across all batuta stack repositories.

use crate::cli::colors as c;
use crate::cli::OutputFormat;
use anyhow::Result;
use std::path::PathBuf;

/// PMAT marker comment embedded in generated hooks for identification.
const PMAT_STACK_MARKER: &str = "# PMAT Standardized Pre-Commit Hook (auto-installed)";

/// Default sovereign AI stack repository names.
const DEFAULT_STACK_REPOS: &[&str] = &[
    "trueno",
    "trueno-graph",
    "trueno-db",
    "trueno-rag",
    "trueno-viz",
    "trueno-zram-core",
    "aprender",
    "paiml-mcp-agent-toolkit",
    "certeza",
    "bashrs",
    "probar",
    "renacer",
    "presentar",
    "pmcp",
];

/// Information about a discovered stack repository.
#[derive(Debug, Clone)]
struct StackRepo {
    name: String,
    path: PathBuf,
    exists: bool,
}

/// Hook status for a single repository.
#[derive(Debug, Clone, PartialEq, Eq)]
enum HookState {
    /// PMAT hook is installed and current
    Installed,
    /// No pre-commit hook present
    Missing,
    /// Hook exists but is not PMAT-managed
    NonPmat,
    /// PMAT hook exists but content differs from current template
    Outdated,
}

impl std::fmt::Display for HookState {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            HookState::Installed => write!(f, "installed"),
            HookState::Missing => write!(f, "missing"),
            HookState::NonPmat => write!(f, "non-pmat"),
            HookState::Outdated => write!(f, "outdated"),
        }
    }
}

/// Discover stack repos from `~/src/<name>`.
///
/// Falls back to default list; each entry includes whether the repo
/// directory (with a `.git` subdirectory) actually exists on disk.
fn discover_stack_repos() -> Vec<StackRepo> {
    let home = std::env::var("HOME").unwrap_or_default();
    let src_dir = PathBuf::from(&home).join("src");

    DEFAULT_STACK_REPOS
        .iter()
        .map(|name| {
            let path = src_dir.join(name);
            let exists = path.join(".git").exists();
            StackRepo {
                name: name.to_string(),
                path,
                exists,
            }
        })
        .collect()
}

/// Generate the standardized pre-commit hook shell script.
fn generate_hook_content() -> String {
    format!(
        r#"#!/bin/sh
{PMAT_STACK_MARKER}
set -e

# 1. Cargo fmt check (Rust projects only)
if [ -f Cargo.toml ]; then
    cargo fmt --all -- --check 2>/dev/null || {{
        echo "cargo fmt failed. Run: cargo fmt --all"
        exit 1
    }}
fi

# 2. Orphan test detection (check for #[test] in files not in module tree)
if command -v pmat >/dev/null 2>&1; then
    pmat comply check --check orphan-tests --quiet 2>/dev/null || true
fi

# 3. TDG baseline update (fast, O(1) check)
if command -v pmat >/dev/null 2>&1 && [ -f .pmat/baseline.json ]; then
    pmat baseline update --quiet 2>/dev/null || true
fi
"#
    )
}

/// Detect the state of a pre-commit hook in a given repository.
fn detect_hook_state(repo_path: &PathBuf) -> HookState {
    let hook_path = repo_path.join(".git/hooks/pre-commit");
    if !hook_path.exists() {
        return HookState::Missing;
    }

    match std::fs::read_to_string(&hook_path) {
        Ok(content) => {
            if !content.contains(PMAT_STACK_MARKER) {
                HookState::NonPmat
            } else if content.trim() == generate_hook_content().trim() {
                HookState::Installed
            } else {
                HookState::Outdated
            }
        }
        Err(_) => HookState::NonPmat,
    }
}

/// Install pre-commit hooks across all sovereign AI stack repos.
///
/// When `update` is true, existing PMAT hooks are overwritten with the
/// latest template.  Non-PMAT hooks are never overwritten.
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub async fn handle_hooks_install_stack(update: bool) -> Result<()> {
    let repos = discover_stack_repos();
    let hook_content = generate_hook_content();

    println!("{}", c::header("Installing PMAT hooks across stack repos"));
    println!();

    let mut installed = 0u32;
    let mut skipped = 0u32;
    let mut missing_repos = 0u32;
    let mut errors = 0u32;

    for repo in &repos {
        if !repo.exists {
            missing_repos += 1;
            println!(
                "  {} {} {}",
                c::dim(&repo.name),
                c::dim("-"),
                c::dim("repo not found")
            );
            continue;
        }

        let hook_path = repo.path.join(".git/hooks/pre-commit");
        let state = detect_hook_state(&repo.path);

        match state {
            HookState::Missing => match write_hook(&hook_path, &hook_content) {
                Ok(()) => {
                    installed += 1;
                    println!(
                        "  {} {} {}",
                        c::pass("installed"),
                        c::label(&repo.name),
                        c::path(hook_path.to_string_lossy().as_ref())
                    );
                }
                Err(e) => {
                    errors += 1;
                    println!("  {} {} - {}", c::fail("error"), c::label(&repo.name), e);
                }
            },
            HookState::NonPmat => {
                skipped += 1;
                println!(
                    "  {} {} - non-PMAT hook exists, skipping",
                    c::warn("skipped"),
                    c::label(&repo.name),
                );
            }
            HookState::Installed => {
                skipped += 1;
                println!(
                    "  {} {} - already up to date",
                    c::skip("skipped"),
                    c::label(&repo.name),
                );
            }
            HookState::Outdated => {
                if update {
                    match write_hook(&hook_path, &hook_content) {
                        Ok(()) => {
                            installed += 1;
                            println!(
                                "  {} {} {}",
                                c::pass("updated"),
                                c::label(&repo.name),
                                c::path(hook_path.to_string_lossy().as_ref())
                            );
                        }
                        Err(e) => {
                            errors += 1;
                            println!("  {} {} - {}", c::fail("error"), c::label(&repo.name), e);
                        }
                    }
                } else {
                    skipped += 1;
                    println!(
                        "  {} {} - outdated, use --update to overwrite",
                        c::warn("skipped"),
                        c::label(&repo.name),
                    );
                }
            }
        }
    }

    println!();
    println!("{}", c::separator());
    println!(
        "  {}: {}  {}: {}  {}: {}  {}: {}",
        c::dim("Installed"),
        c::number(&installed.to_string()),
        c::dim("Skipped"),
        c::number(&skipped.to_string()),
        c::dim("Missing repos"),
        c::number(&missing_repos.to_string()),
        c::dim("Errors"),
        if errors > 0 {
            format!("{}{}{}", c::RED, errors, c::RESET)
        } else {
            c::number(&errors.to_string())
        },
    );

    if errors > 0 {
        Err(anyhow::anyhow!("{} hook(s) failed to install", errors))
    } else {
        Ok(())
    }
}

/// Show hook installation status across all sovereign AI stack repos.
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub async fn handle_hooks_status_stack(format: &OutputFormat) -> Result<()> {
    let repos = discover_stack_repos();

    match format {
        OutputFormat::Json => print_status_json(&repos),
        _ => print_status_table(&repos),
    }

    Ok(())
}

/// Print stack hook status as a table.
fn print_status_table(repos: &[StackRepo]) {
    println!("{}", c::header("Stack Hook Status"));
    println!();
    println!(
        "  {:<28} {:<12} {}",
        c::subheader("Repository"),
        c::subheader("Status"),
        c::subheader("Path"),
    );
    println!("  {}", c::separator());

    let mut installed_count = 0u32;
    let mut total_present = 0u32;

    for repo in repos {
        if !repo.exists {
            println!(
                "  {:<28} {:<12} {}",
                c::dim(&repo.name),
                c::dim("not found"),
                c::dim("-"),
            );
            continue;
        }

        total_present += 1;
        let state = detect_hook_state(&repo.path);
        let status_str = match &state {
            HookState::Installed => {
                installed_count += 1;
                c::pass("installed")
            }
            HookState::Missing => c::fail("missing"),
            HookState::NonPmat => format!("{}{}non-pmat{}", c::YELLOW, "", c::RESET),
            HookState::Outdated => c::warn("outdated"),
        };

        let hook_path = repo.path.join(".git/hooks/pre-commit");
        let path_str = if hook_path.exists() {
            c::path(hook_path.to_string_lossy().as_ref())
        } else {
            c::dim("-")
        };

        // Use raw name (no ANSI) for fixed-width alignment, then print colored
        println!("  {:<28} {} {}", &repo.name, status_str, path_str,);
    }

    println!();
    println!(
        "  {}/{} stack repos have PMAT hooks installed",
        c::number(&installed_count.to_string()),
        c::number(&total_present.to_string()),
    );
}

/// Print stack hook status as JSON.
fn print_status_json(repos: &[StackRepo]) {
    let mut entries = Vec::new();
    for repo in repos {
        let (status, path_str) = if !repo.exists {
            ("not_found".to_string(), String::new())
        } else {
            let state = detect_hook_state(&repo.path);
            let hook_path = repo.path.join(".git/hooks/pre-commit");
            (
                state.to_string(),
                if hook_path.exists() {
                    hook_path.to_string_lossy().into_owned()
                } else {
                    String::new()
                },
            )
        };
        entries.push(format!(
            r#"    {{"name": "{}", "status": "{}", "path": "{}"}}"#,
            repo.name, status, path_str
        ));
    }
    println!("[\n{}\n]", entries.join(",\n"));
}

/// Write hook content to disk atomically (temp + rename, CB-1334).
fn write_hook(hook_path: &PathBuf, content: &str) -> Result<()> {
    // Ensure the hooks directory exists
    if let Some(parent) = hook_path.parent() {
        std::fs::create_dir_all(parent)?;
    }

    // Atomic write: temp file + rename
    let tmp_path = hook_path.with_extension("tmp");
    std::fs::write(&tmp_path, content)?;

    #[cfg(unix)]
    {
        use std::os::unix::fs::PermissionsExt;
        let perms = std::fs::Permissions::from_mode(0o755);
        std::fs::set_permissions(&tmp_path, perms)?;
    }

    std::fs::rename(&tmp_path, hook_path)?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_discover_stack_repos() {
        let repos = discover_stack_repos();
        assert!(!repos.is_empty());
        // Every entry should have a non-empty name
        for repo in &repos {
            assert!(!repo.name.is_empty());
            assert!(!repo.path.as_os_str().is_empty());
        }
    }

    #[test]
    fn test_default_repos_count() {
        assert_eq!(DEFAULT_STACK_REPOS.len(), 14);
        assert!(DEFAULT_STACK_REPOS.contains(&"trueno"));
        assert!(DEFAULT_STACK_REPOS.contains(&"paiml-mcp-agent-toolkit"));
        assert!(DEFAULT_STACK_REPOS.contains(&"aprender"));
    }

    #[test]
    fn test_generate_hook_content() {
        let content = generate_hook_content();
        // Must start with shebang
        assert!(content.starts_with("#!/bin/sh"));
        // Must contain the PMAT marker
        assert!(content.contains(PMAT_STACK_MARKER));
        // Must contain cargo fmt check
        assert!(content.contains("cargo fmt --all -- --check"));
        // Must contain orphan test detection
        assert!(content.contains("orphan-tests"));
        // Must contain TDG baseline update
        assert!(content.contains("pmat baseline update"));
    }

    #[test]
    fn test_hook_marker_detection() {
        // A hook containing the marker should be detected
        let content = generate_hook_content();
        assert!(content.contains(PMAT_STACK_MARKER));

        // Some random script should not have the marker
        let other = "#!/bin/sh\necho hello\n";
        assert!(!other.contains(PMAT_STACK_MARKER));
    }

    #[test]
    fn test_detect_hook_state_missing() {
        // A path that doesn't exist should yield Missing
        let fake = PathBuf::from("/tmp/pmat-test-nonexistent-repo-xyz");
        assert_eq!(detect_hook_state(&fake), HookState::Missing);
    }

    #[test]
    fn test_hook_state_display() {
        assert_eq!(HookState::Installed.to_string(), "installed");
        assert_eq!(HookState::Missing.to_string(), "missing");
        assert_eq!(HookState::NonPmat.to_string(), "non-pmat");
        assert_eq!(HookState::Outdated.to_string(), "outdated");
    }

    #[test]
    fn test_write_and_detect_hook() {
        let tmp = std::env::temp_dir().join("pmat-hook-test");
        let git_hooks = tmp.join(".git/hooks");
        let _ = std::fs::create_dir_all(&git_hooks);
        let hook_path = git_hooks.join("pre-commit");

        let content = generate_hook_content();

        // Write the hook
        write_hook(&hook_path, &content).unwrap();

        // Detect state - should be Installed
        assert_eq!(detect_hook_state(&tmp), HookState::Installed);

        // Modify the hook slightly to make it outdated
        std::fs::write(&hook_path, format!("{}\n# extra", content)).unwrap();
        assert_eq!(detect_hook_state(&tmp), HookState::Outdated);

        // Write a non-PMAT hook
        std::fs::write(&hook_path, "#!/bin/sh\necho hello\n").unwrap();
        assert_eq!(detect_hook_state(&tmp), HookState::NonPmat);

        // Cleanup
        let _ = std::fs::remove_dir_all(&tmp);
    }
}