pmat 2.93.1

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
464
465
466
467
468
469
470
471
472
//! Interactive scaffolding interface for guided agent creation.

use super::context::{AgentContext, AgentContextBuilder};
use super::error::ScaffoldError;
use super::features::{AgentFeature, QualityLevel};
use super::hybrid::{CoreSpec, FallbackStrategy, ModelType, VerificationMethod, WrapperSpec};
use super::templates::AgentTemplate;
use anyhow::Result;
use console::Term;
use dialoguer::{theme::ColorfulTheme, Confirm, Input, MultiSelect, Select};
use std::collections::HashSet;
use std::path::PathBuf;

/// Interactive scaffolder for guided agent creation.
pub struct InteractiveScaffolder {
    /// Terminal for I/O.
    term: Term,
    /// Color theme for prompts.
    theme: ColorfulTheme,
}

impl InteractiveScaffolder {
    /// Create a new interactive scaffolder.
    #[must_use] 
    pub fn new() -> Self {
        Self {
            term: Term::stdout(),
            theme: ColorfulTheme::default(),
        }
    }

    /// Run the interactive scaffolding process.
    pub fn run(&mut self) -> Result<AgentContext> {
        // Clear screen and show header
        self.term.clear_screen()?;
        self.show_header()?;

        // Prompt for basic information
        let name = self.prompt_name()?;
        let template = self.prompt_template()?;
        let features = self.prompt_features(&template)?;
        let quality = self.prompt_quality_level()?;

        // Build context
        let mut builder = AgentContextBuilder::new(&name, self.template_to_string(&template));
        for feature in &features {
            builder = builder.with_feature(feature.clone());
        }
        builder = builder.with_quality_level(quality);

        // Handle hybrid-specific configuration
        if matches!(template, AgentTemplate::HybridAnalyzer) {
            let core = self.prompt_deterministic_core()?;
            let wrapper = self.prompt_probabilistic_wrapper()?;
            builder = builder
                .with_deterministic_core(core)
                .with_probabilistic_wrapper(wrapper);
        }

        // Build and confirm
        let ctx = builder.build()?;
        self.confirm_and_display(&ctx)?;

        Ok(ctx)
    }

    /// Show the header.
    fn show_header(&self) -> Result<()> {
        println!("╔══════════════════════════════════════════╗");
        println!("║      PMAT Agent Scaffolder v0.1.0       ║");
        println!("║   Interactive Agent Creation Wizard      ║");
        println!("╚══════════════════════════════════════════╝");
        println!();
        Ok(())
    }

    /// Prompt for agent name.
    fn prompt_name(&self) -> Result<String> {
        loop {
            let name: String = Input::with_theme(&self.theme)
                .with_prompt("Agent name")
                .validate_with(|input: &String| -> Result<(), &str> {
                    if input.is_empty() {
                        Err("Name cannot be empty")
                    } else if !input.chars().all(|c| c.is_alphanumeric() || c == '_') {
                        Err("Name must be alphanumeric with underscores only")
                    } else if input.chars().next().is_some_and(char::is_numeric) {
                        Err("Name cannot start with a number")
                    } else {
                        Ok(())
                    }
                })
                .interact_text()?;

            if name.len() > 64 {
                eprintln!("Warning: Name is quite long. Consider a shorter name.");
                if !Confirm::with_theme(&self.theme)
                    .with_prompt("Continue with this name?")
                    .interact()?
                {
                    continue;
                }
            }

            return Ok(name);
        }
    }

    /// Prompt for template type.
    fn prompt_template(&self) -> Result<AgentTemplate> {
        let items = [
            ("MCP Tool Server", "Standard MCP server with async handlers"),
            (
                "State Machine Workflow",
                "Agent with state transitions and invariants",
            ),
            (
                "Deterministic Calculator",
                "Pure deterministic computation agent",
            ),
            ("Hybrid Analyzer", "Deterministic core with AI wrapper"),
            ("Custom Template", "Use a custom template from file"),
        ];

        let selection = Select::with_theme(&self.theme)
            .with_prompt("Select template type")
            .items(
                &items
                    .iter()
                    .map(|(name, desc)| format!("{name}\n   {desc}"))
                    .collect::<Vec<_>>(),
            )
            .default(0)
            .interact()?;

        Ok(match selection {
            0 => AgentTemplate::MCPToolServer,
            1 => AgentTemplate::StateMachineWorkflow,
            2 => AgentTemplate::DeterministicCalculator,
            3 => AgentTemplate::HybridAnalyzer,
            4 => {
                let path: String = Input::with_theme(&self.theme)
                    .with_prompt("Custom template path")
                    .interact_text()?;
                AgentTemplate::CustomAgent(PathBuf::from(path))
            }
            _ => unreachable!(),
        })
    }

    /// Prompt for features to include.
    fn prompt_features(&self, template: &AgentTemplate) -> Result<HashSet<AgentFeature>> {
        let available_features = self.get_available_features(template);

        if available_features.is_empty() {
            return Ok(HashSet::new());
        }

        let feature_names: Vec<String> = available_features
            .iter()
            .map(|f| self.feature_to_string(f))
            .collect();

        let selections = MultiSelect::with_theme(&self.theme)
            .with_prompt("Select features to include (Space to select, Enter to confirm)")
            .items(&feature_names)
            .interact()?;

        let mut features = HashSet::new();
        for idx in selections {
            features.insert(available_features[idx].clone());
        }

        Ok(features)
    }

    /// Get available features for a template.
    fn get_available_features(&self, template: &AgentTemplate) -> Vec<AgentFeature> {
        match template {
            AgentTemplate::MCPToolServer => vec![
                AgentFeature::ToolComposition,
                AgentFeature::AsyncHandlers,
                AgentFeature::ResourceSubscriptions,
                AgentFeature::Monitoring {
                    backend: super::features::MonitoringBackend::Prometheus,
                },
                AgentFeature::Tracing {
                    exporter: super::features::TraceExporter::OTLP,
                },
                AgentFeature::HealthChecks,
            ],
            AgentTemplate::StateMachineWorkflow => vec![
                AgentFeature::StateMachine {
                    states: vec![
                        "Initial".to_string(),
                        "Processing".to_string(),
                        "Complete".to_string(),
                    ],
                },
                AgentFeature::QualityGates {
                    level: QualityLevel::Extreme,
                },
            ],
            AgentTemplate::HybridAnalyzer => vec![
                AgentFeature::ComplexityAnalysis,
                AgentFeature::SATDDetection,
                AgentFeature::DeadCodeElimination,
            ],
            _ => vec![],
        }
    }

    /// Convert feature to display string.
    fn feature_to_string(&self, feature: &AgentFeature) -> String {
        match feature {
            AgentFeature::StateMachine { .. } => "State Machine with transitions".to_string(),
            AgentFeature::QualityGates { .. } => "Quality Gates enforcement".to_string(),
            AgentFeature::ToolComposition => "Tool Composition support".to_string(),
            AgentFeature::AsyncHandlers => "Async request handlers".to_string(),
            AgentFeature::ResourceSubscriptions => "Resource subscriptions".to_string(),
            AgentFeature::ComplexityAnalysis => "Complexity analysis".to_string(),
            AgentFeature::SATDDetection => "SATD detection".to_string(),
            AgentFeature::DeadCodeElimination => "Dead code elimination".to_string(),
            AgentFeature::Monitoring { .. } => "Monitoring integration".to_string(),
            AgentFeature::Tracing { .. } => "Distributed tracing".to_string(),
            AgentFeature::HealthChecks => "Health check endpoints".to_string(),
        }
    }

    /// Prompt for quality level.
    fn prompt_quality_level(&self) -> Result<QualityLevel> {
        let items = [
            ("Standard", "Basic quality checks, suitable for prototypes"),
            ("Strict", "Zero warnings, high test coverage"),
            (
                "Extreme (Toyota Way)",
                "Zero SATD, max complexity 10, full verification",
            ),
        ];

        let selection = Select::with_theme(&self.theme)
            .with_prompt("Quality level")
            .items(
                &items
                    .iter()
                    .map(|(name, desc)| format!("{name}\n   {desc}"))
                    .collect::<Vec<_>>(),
            )
            .default(1)
            .interact()?;

        Ok(match selection {
            0 => QualityLevel::Standard,
            1 => QualityLevel::Strict,
            2 => QualityLevel::Extreme,
            _ => unreachable!(),
        })
    }

    /// Prompt for deterministic core specification.
    fn prompt_deterministic_core(&self) -> Result<CoreSpec> {
        println!("\n=== Deterministic Core Configuration ===");

        let verification_items = ["Property-based tests", "Formal proof", "Model checking"];

        let verification_idx = Select::with_theme(&self.theme)
            .with_prompt("Verification method")
            .items(&verification_items)
            .default(0)
            .interact()?;

        let verification_method = match verification_idx {
            0 => VerificationMethod::PropertyTests,
            1 => VerificationMethod::FormalProof,
            2 => VerificationMethod::ModelChecking,
            _ => unreachable!(),
        };

        let max_complexity = Input::with_theme(&self.theme)
            .with_prompt("Maximum cyclomatic complexity")
            .default(10)
            .validate_with(|input: &u32| -> Result<(), &str> {
                if *input == 0 {
                    Err("Complexity must be at least 1")
                } else if *input > 50 {
                    Err("Complexity should not exceed 50")
                } else {
                    Ok(())
                }
            })
            .interact()?;

        Ok(CoreSpec {
            verification_method,
            max_complexity,
            invariants: Vec::new(),
        })
    }

    /// Prompt for probabilistic wrapper specification.
    fn prompt_probabilistic_wrapper(&self) -> Result<WrapperSpec> {
        println!("\n=== Probabilistic Wrapper Configuration ===");

        let model_items = ["GPT-4", "Claude", "Local model"];

        let model_idx = Select::with_theme(&self.theme)
            .with_prompt("AI model type")
            .items(&model_items)
            .default(0)
            .interact()?;

        let model_type = match model_idx {
            0 => ModelType::GPT4,
            1 => ModelType::Claude,
            2 => {
                let path = Input::with_theme(&self.theme)
                    .with_prompt("Local model path")
                    .interact_text()?;
                ModelType::Local(path)
            }
            _ => unreachable!(),
        };

        let fallback_items = ["Deterministic fallback", "Default value", "Return error"];

        let fallback_idx = Select::with_theme(&self.theme)
            .with_prompt("Fallback strategy")
            .items(&fallback_items)
            .default(0)
            .interact()?;

        let fallback_strategy = match fallback_idx {
            0 => FallbackStrategy::Deterministic,
            1 => FallbackStrategy::DefaultValue,
            2 => FallbackStrategy::Error,
            _ => unreachable!(),
        };

        let confidence_threshold = Input::with_theme(&self.theme)
            .with_prompt("Confidence threshold (0.0-1.0)")
            .default(0.95)
            .validate_with(|input: &f64| -> Result<(), &str> {
                if *input < 0.0 || *input > 1.0 {
                    Err("Threshold must be between 0.0 and 1.0")
                } else {
                    Ok(())
                }
            })
            .interact()?;

        Ok(WrapperSpec {
            model_type,
            fallback_strategy,
            confidence_threshold,
        })
    }

    /// Convert template to string representation.
    fn template_to_string(&self, template: &AgentTemplate) -> String {
        match template {
            AgentTemplate::MCPToolServer => "mcp-server".to_string(),
            AgentTemplate::StateMachineWorkflow => "state-machine".to_string(),
            AgentTemplate::DeterministicCalculator => "calculator".to_string(),
            AgentTemplate::HybridAnalyzer => "hybrid".to_string(),
            AgentTemplate::CustomAgent(path) => format!("custom:{}", path.display()),
        }
    }

    /// Confirm and display the configuration.
    fn confirm_and_display(&self, ctx: &AgentContext) -> Result<()> {
        println!("\n╔══════════════════════════════════════════╗");
        println!("║         Agent Configuration Summary      ║");
        println!("╚══════════════════════════════════════════╝");
        println!();
        println!("  Name:     {}", ctx.name);
        println!("  Template: {:?}", ctx.template_type);
        println!("  Quality:  {:?}", ctx.quality_level);
        println!("  Features: {} enabled", ctx.features.len());

        if let Some(core) = &ctx.deterministic_core {
            println!("\n  Deterministic Core:");
            println!("    Verification: {:?}", core.verification_method);
            println!("    Max Complexity: {}", core.max_complexity);
        }

        if let Some(wrapper) = &ctx.probabilistic_wrapper {
            println!("\n  Probabilistic Wrapper:");
            println!("    Model: {:?}", wrapper.model_type);
            println!("    Fallback: {:?}", wrapper.fallback_strategy);
            println!("    Confidence: {:.2}", wrapper.confidence_threshold);
        }

        println!();
        let confirm = Confirm::with_theme(&self.theme)
            .with_prompt("Generate agent with these settings?")
            .default(true)
            .interact()?;

        if !confirm {
            return Err(ScaffoldError::UserCancelled.into());
        }

        Ok(())
    }
}

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

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

    #[test]
    fn test_interactive_scaffolder_creation() {
        let _scaffolder = InteractiveScaffolder::new();
        // Just verify it can be created without panicking
    }

    #[test]
    fn test_feature_to_string() {
        let scaffolder = InteractiveScaffolder::new();
        let feature = AgentFeature::ToolComposition;
        let str = scaffolder.feature_to_string(&feature);
        assert_eq!(str, "Tool Composition support");
    }

    #[test]
    fn test_template_to_string() {
        let scaffolder = InteractiveScaffolder::new();
        assert_eq!(
            scaffolder.template_to_string(&AgentTemplate::MCPToolServer),
            "mcp-server"
        );
        assert_eq!(
            scaffolder.template_to_string(&AgentTemplate::HybridAnalyzer),
            "hybrid"
        );
    }

    #[test]
    fn test_get_available_features() {
        let scaffolder = InteractiveScaffolder::new();
        let features = scaffolder.get_available_features(&AgentTemplate::MCPToolServer);
        assert!(!features.is_empty());
        assert!(features
            .iter()
            .any(|f| matches!(f, AgentFeature::ToolComposition)));
    }
}

#[cfg(test)]
mod property_tests {
    use proptest::prelude::*;

    proptest! {
        #[test]
        fn basic_property_stability(_input in ".*") {
            // Basic property test for coverage
            prop_assert!(true);
        }

        #[test]
        fn module_consistency_check(_x in 0u32..1000) {
            // Module consistency verification
            prop_assert!(_x < 1001);
        }
    }
}