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
//! Command dispatch and handler functions for hooks subcommands

#![cfg_attr(coverage_nightly, coverage(off))]

use super::hooks_command::HooksCommand;
use super::tdg_hooks::install_tdg_hooks_wrapper;
use super::types::{HookStatus, HookVerificationResult};
use crate::cli::colors as c;
use crate::cli::commands::HooksCommands;
use anyhow::Result;

/// Handle hooks subcommand
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub async fn handle_hooks_command(cmd: &HooksCommands) -> Result<()> {
    let hooks_cmd = HooksCommand::for_current_repo()?;

    match cmd {
        HooksCommands::Init {
            interactive,
            force,
            backup,
            tdg_enforcement,
        } => handle_install(&hooks_cmd, *force, *backup, *interactive, *tdg_enforcement).await,
        HooksCommands::Install {
            interactive,
            force,
            backup,
            tdg_enforcement,
            stack,
            update,
        } => {
            if *stack {
                return crate::cli::handlers::hooks_stack_handler::handle_hooks_install_stack(
                    *update,
                )
                .await;
            }
            handle_install(&hooks_cmd, *force, *backup, *interactive, *tdg_enforcement).await
        }
        HooksCommands::Uninstall { restore_backup } => {
            handle_uninstall(&hooks_cmd, *restore_backup).await
        }
        HooksCommands::Status { stack, format } => {
            if *stack {
                return crate::cli::handlers::hooks_stack_handler::handle_hooks_status_stack(
                    format,
                )
                .await;
            }
            handle_status(&hooks_cmd).await
        }
        HooksCommands::Verify { fix } => handle_verify(&hooks_cmd, *fix).await,
        HooksCommands::Refresh => handle_refresh(&hooks_cmd).await,
        HooksCommands::Run {
            all_files,
            verbose,
            cache,
        } => handle_run(&hooks_cmd, *all_files, *verbose, *cache).await,
        HooksCommands::Cache { action } => super::cache_handlers::handle_cache(action).await,
    }
}

/// Handle hooks install command
async fn handle_install(
    hooks_cmd: &HooksCommand,
    force: bool,
    backup: bool,
    interactive: bool,
    tdg_enforcement: bool,
) -> Result<()> {
    // Handle TDG enforcement installation (Sprint 66 Phase 3)
    if tdg_enforcement {
        println!(
            "{}",
            c::label("Installing PMAT hooks with TDG enforcement...")
        );
        return install_tdg_hooks_wrapper().await;
    }

    println!("{}", c::label("Installing pre-commit hooks..."));
    if interactive {
        println!("  {}", c::dim("Interactive mode enabled"));
    }
    if force {
        println!("  {}", c::dim("Force installation enabled"));
    }
    // Don't print backup message here - only print after actual backup happens

    let result = hooks_cmd.install(force, backup, interactive).await?;

    if result.success {
        if result.backup_created {
            println!(
                "  {} Backup created: {}",
                c::pass(""),
                c::path(".git/hooks/pre-commit.pmat-backup")
            );
        }
        println!("{}", c::pass(&result.message));
    } else {
        println!("{}", c::fail(&result.message));
        return Err(anyhow::anyhow!(result.message));
    }

    Ok(())
}

/// Handle hooks uninstall command
async fn handle_uninstall(hooks_cmd: &HooksCommand, restore_backup: bool) -> Result<()> {
    println!("{}", c::label("Uninstalling pre-commit hooks..."));
    if restore_backup {
        println!("  {}", c::dim("Restoring backup enabled"));
    }

    let result = hooks_cmd.uninstall(restore_backup).await?;

    if result.success {
        if result.backup_restored {
            println!("  {}", c::pass("Backup restored"));
        }
        println!("{}", c::pass(&result.message));
    } else {
        println!("{}", c::fail(&result.message));
        return Err(anyhow::anyhow!(result.message));
    }

    Ok(())
}

/// Handle hooks status command
async fn handle_status(hooks_cmd: &HooksCommand) -> Result<()> {
    let status = hooks_cmd.status().await?;

    println!("{}", c::header("Pre-commit Hook Status:"));
    println!(
        "  {}: {}",
        c::dim("Installed"),
        if status.installed {
            c::pass("Yes")
        } else {
            c::fail("No")
        }
    );

    if status.installed {
        print_installed_status(&status);
    }

    Ok(())
}

/// Print detailed status for installed hook
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub(crate) fn print_installed_status(status: &HookStatus) {
    println!(
        "  {}: {}",
        c::dim("PMAT-managed"),
        if status.is_pmat_managed {
            c::pass("Yes")
        } else {
            c::fail("No")
        }
    );
    println!(
        "  {}: {}",
        c::dim("Config up-to-date"),
        if status.config_up_to_date {
            c::pass("Yes")
        } else {
            c::warn("No")
        }
    );

    if let Some(last_updated) = &status.last_updated {
        println!("  {}: {last_updated}", c::dim("Last updated"));
    }

    if let Some(preview) = &status.hook_content_preview {
        println!("\n  {}:", c::dim("Hook preview"));
        for line in preview.lines() {
            println!("    {}", c::dim(line));
        }
    }
}

/// Handle hooks verify command
async fn handle_verify(hooks_cmd: &HooksCommand, fix: bool) -> Result<()> {
    println!("{}", c::label("Verifying pre-commit hooks..."));

    if fix {
        println!("  {}", c::dim("Auto-fix enabled"));
    }

    let result = hooks_cmd.verify(fix).await?;

    print_verification_issues(&result);
    print_verification_fixes(&result);

    if result.is_valid {
        println!("{}", c::pass("Pre-commit hooks verified successfully"));
    } else {
        println!("{}", c::fail("Pre-commit hooks verification failed"));
        if !fix {
            println!(
                "   {}",
                c::dim("Run with --fix to attempt automatic repairs")
            );
        }
        return Err(anyhow::anyhow!("Hook verification failed"));
    }

    Ok(())
}

/// Print verification issues
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub(crate) fn print_verification_issues(result: &HookVerificationResult) {
    if !result.issues.is_empty() {
        println!("  {}:", c::subheader("Issues found"));
        for issue in &result.issues {
            println!("    {}", c::warn(issue));
        }
    }
}

/// Print verification fixes applied
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub(crate) fn print_verification_fixes(result: &HookVerificationResult) {
    if !result.fixes_applied.is_empty() {
        println!("  {}:", c::subheader("Fixes applied"));
        for fix_msg in &result.fixes_applied {
            println!("    {}", c::pass(fix_msg));
        }
    }
}

/// Handle hooks refresh command
async fn handle_refresh(hooks_cmd: &HooksCommand) -> Result<()> {
    println!(
        "{}",
        c::label("Refreshing pre-commit hooks from configuration...")
    );

    let result = hooks_cmd.refresh().await?;

    if result.success {
        if result.config_changes_detected {
            println!("  {}", c::pass("Configuration changes detected"));
        }
        if result.hook_updated {
            println!("  {}", c::pass("Hook updated with new configuration"));
        }
        println!("{}", c::pass(&result.message));
    } else {
        println!("{}", c::fail(&result.message));
        return Err(anyhow::anyhow!(result.message));
    }

    Ok(())
}

/// Handle hooks run command with O(1) cache check
async fn handle_run(
    hooks_cmd: &HooksCommand,
    all_files: bool,
    verbose: bool,
    use_cache: bool,
) -> Result<()> {
    let start_time = std::time::Instant::now();

    // O(1) cache check if enabled
    if use_cache {
        let project_root = std::env::current_dir()?;
        let cache_manager = crate::tdg::hooks_cache::HooksCacheManager::new(&project_root);

        // Initialize cache if it doesn't exist
        if !project_root.join(".pmat/hooks-cache").exists() {
            let _ = cache_manager.init();
        }

        match cache_manager.check() {
            Ok(crate::tdg::hooks_cache::CacheCheckResult::Hit { result, cached_at }) => {
                let elapsed = start_time.elapsed();

                if verbose {
                    println!("{} O(1) Cache HIT - Skipping full analysis", c::pass(""));
                    println!("   {}: {:?}", c::dim("Cached result"), result);
                    println!(
                        "   {}: {}",
                        c::dim("Cached at"),
                        cached_at.format("%Y-%m-%d %H:%M:%S UTC")
                    );
                    println!(
                        "   {}: {:.2}ms",
                        c::dim("Check time"),
                        elapsed.as_secs_f64() * 1000.0
                    );
                }

                // Record the cache hit
                let _ = cache_manager.record_run(true, elapsed.as_millis() as u64);

                match result {
                    crate::tdg::hooks_cache::CacheResult::Pass => {
                        println!(
                            "{} {}",
                            c::pass("All quality gates passed"),
                            c::dim("(cached)")
                        );
                        return Ok(());
                    }
                    crate::tdg::hooks_cache::CacheResult::Fail => {
                        println!("{} {}", c::fail("Quality gates failed"), c::dim("(cached)"));
                        return Err(anyhow::anyhow!("Pre-commit checks failed (cached)"));
                    }
                    crate::tdg::hooks_cache::CacheResult::Warn => {
                        println!(
                            "{} {}",
                            c::warn("Quality gates passed with warnings"),
                            c::dim("(cached)")
                        );
                        return Ok(());
                    }
                }
            }
            Ok(crate::tdg::hooks_cache::CacheCheckResult::Miss { reason }) => {
                if verbose {
                    println!("{} Cache MISS: {}", c::dim(""), reason);
                    println!("   {}", c::dim("Running full analysis..."));
                }
            }
            Ok(crate::tdg::hooks_cache::CacheCheckResult::Partial { .. }) => {
                if verbose {
                    println!(
                        "{}",
                        c::warn("Partial cache hit - running remaining gates...")
                    );
                }
            }
            Err(e) => {
                if verbose {
                    println!("{}", c::warn(&format!("Cache check failed: {}", e)));
                    println!("   {}", c::dim("Running full analysis..."));
                }
            }
        }
    }

    // Run full hooks
    let result = hooks_cmd.run(all_files, verbose).await?;
    let elapsed = start_time.elapsed();

    if verbose {
        println!();
        println!("{}", c::subheader("Results:"));
        println!(
            "  {}: {}",
            c::dim("Checks passed"),
            c::number(&result.checks_passed.to_string())
        );
        println!(
            "  {}: {}",
            c::dim("Checks failed"),
            if result.checks_failed > 0 {
                format!("{}{}{}", c::RED, result.checks_failed, c::RESET)
            } else {
                result.checks_failed.to_string()
            }
        );
        println!(
            "  {}: {:.2}ms",
            c::dim("Duration"),
            elapsed.as_secs_f64() * 1000.0
        );
        println!();
        println!("{}:", c::dim("Output"));
        println!("{}", result.output);
    } else {
        // Non-verbose: just print output
        println!("{}", result.output);
    }

    // Update cache if enabled
    if use_cache {
        let project_root = std::env::current_dir()?;
        let cache_manager = crate::tdg::hooks_cache::HooksCacheManager::new(&project_root);

        let cache_result = if result.success {
            crate::tdg::hooks_cache::CacheResult::Pass
        } else {
            crate::tdg::hooks_cache::CacheResult::Fail
        };

        // Update cache with results
        if let Err(e) = cache_manager.update(cache_result, std::collections::HashMap::new()) {
            if verbose {
                println!("{}", c::warn(&format!("Failed to update cache: {}", e)));
            }
        }

        // Record the cache miss run
        let _ = cache_manager.record_run(false, elapsed.as_millis() as u64);
    }

    if result.success {
        println!();
        println!("{}", c::pass("All pre-commit checks passed"));
        Ok(())
    } else {
        println!();
        println!("{}", c::fail("Pre-commit checks failed"));
        Err(anyhow::anyhow!("Pre-commit checks failed"))
    }
}