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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
#![cfg_attr(
    all(coverage_nightly, not(coverage_attr_stable)),
    feature(coverage_attribute)
)]
#![cfg_attr(coverage_nightly, coverage(off))]
use clap::{Parser, Subcommand};
use pmat::agents::analyzer_actor::AnalyzerActor;
use pmat::agents::registry::AgentRegistry;
use pmat::agents::transformer_actor::TransformerActor;
use pmat::agents::validator_actor::ValidatorActor;
use pmat::mcp_integration::server::{McpServer, ServerConfig};
use pmat::workflow::dsl::DslCompiler;
use pmat::workflow::{DefaultWorkflowExecutor, WorkflowContext, WorkflowExecutor};
use std::sync::Arc;
use std::time::Duration;
use tokio::fs;

#[derive(Parser)]
#[command(name = "pmat-agent")]
#[command(about = "PMAT Agent System - Enterprise-grade agent orchestration", long_about = None)]
struct Cli {
    #[command(subcommand)]
    command: Commands,
}

#[derive(Subcommand)]
enum Commands {
    /// Start the MCP server
    Serve {
        /// Bind address for TCP server
        #[arg(short, long, default_value = "127.0.0.1:3000")]
        bind: String,

        /// Unix socket path
        #[arg(short = 'u', long)]
        socket: Option<String>,

        /// Use stdio instead of network
        #[arg(long)]
        stdio: bool,

        /// Maximum concurrent connections
        #[arg(long, default_value_t = 100)]
        max_connections: usize,
    },

    /// Execute a workflow
    Execute {
        /// Workflow file path (YAML/JSON)
        #[arg(short, long)]
        file: String,

        /// Input parameters (JSON)
        #[arg(short, long)]
        params: Option<String>,

        /// Timeout in seconds
        #[arg(short, long)]
        timeout: Option<u64>,
    },

    /// Validate a workflow
    Validate {
        /// Workflow file path
        #[arg(short, long)]
        file: String,
    },

    /// Analyze code quality
    Analyze {
        /// Source code file or directory
        #[arg(short, long)]
        path: String,

        /// Programming language
        #[arg(short, long)]
        language: String,

        /// Output format (json, text, html)
        #[arg(short, long, default_value = "text")]
        output: String,
    },

    /// Run quality gates
    QualityGate {
        /// Source path
        #[arg(short, long)]
        path: String,

        /// Language
        #[arg(short, long)]
        language: String,

        /// Max complexity threshold
        #[arg(long, default_value_t = 10)]
        max_complexity: u32,

        /// Max SATD items
        #[arg(long, default_value_t = 0)]
        max_satd: usize,

        /// Fail on violation
        #[arg(long)]
        fail_on_violation: bool,
    },

    /// Show system info
    Info,
}

#[actix_rt::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    // Initialize logging - use tracing instead of env_logger
    tracing_subscriber::fmt::init();

    let cli = Cli::parse();

    // Initialize agent registry
    let registry = Arc::new(AgentRegistry::new());
    initialize_agents(&registry).await?;

    match cli.command {
        Commands::Serve {
            bind,
            socket,
            stdio,
            max_connections,
        } => serve_mcp(registry, bind, socket, stdio, max_connections).await?,
        Commands::Execute {
            file,
            params,
            timeout,
        } => execute_workflow(registry, file, params, timeout).await?,
        Commands::Validate { file } => validate_workflow(file).await?,
        Commands::Analyze {
            path,
            language,
            output,
        } => analyze_code(registry, path, language, output).await?,
        Commands::QualityGate {
            path,
            language,
            max_complexity,
            max_satd,
            fail_on_violation,
        } => {
            run_quality_gate(
                registry,
                path,
                language,
                max_complexity,
                max_satd,
                fail_on_violation,
            )
            .await?
        }
        Commands::Info => show_info().await?,
    }

    Ok(())
}

#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
async fn initialize_agents(
    registry: &Arc<AgentRegistry>,
) -> Result<(), Box<dyn std::error::Error>> {
    use pmat::agents::orchestrator_actor::OrchestratorActor;

    // Register core agents
    registry
        .register("analyzer", Arc::new(AnalyzerActor::default()))
        .await;
    registry
        .register("transformer", Arc::new(TransformerActor::default()))
        .await;
    registry
        .register("validator", Arc::new(ValidatorActor::default()))
        .await;
    registry
        .register("orchestrator", Arc::new(OrchestratorActor::new()))
        .await;

    println!(
        "✓ Initialized {} agents",
        registry.list_agents().await.len()
    );

    Ok(())
}

#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
async fn serve_mcp(
    registry: Arc<AgentRegistry>,
    bind: String,
    socket: Option<String>,
    stdio: bool,
    max_connections: usize,
) -> Result<(), Box<dyn std::error::Error>> {
    let config = ServerConfig {
        name: "PMAT Agent Server".to_string(),
        bind_address: bind.clone(),
        unix_socket: socket.clone(),
        max_connections,
        ..Default::default()
    };

    let server = McpServer::new(registry, config)?;
    server.register_defaults().await?;

    println!("🚀 PMAT Agent Server v{}", env!("CARGO_PKG_VERSION"));
    println!("   Protocol: MCP {}", pmat::mcp_integration::MCP_VERSION);

    if stdio {
        println!("📝 Using stdio transport");
        server.run_stdio().await?;
    } else if let Some(socket_path) = socket {
        println!("🔌 Listening on Unix socket: {}", socket_path);
        server.run_unix().await?;
    } else {
        println!("🌐 Listening on TCP: {}", bind);
        server.run_tcp().await?;
    }

    Ok(())
}

#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
async fn execute_workflow(
    registry: Arc<AgentRegistry>,
    file: String,
    params: Option<String>,
    timeout: Option<u64>,
) -> Result<(), Box<dyn std::error::Error>> {
    println!("📋 Loading workflow: {}", file);

    let content = fs::read_to_string(&file).await?;
    let mut workflow = DslCompiler::compile(&content)?;

    if let Some(timeout_secs) = timeout {
        workflow.timeout = Some(Duration::from_secs(timeout_secs));
    }

    println!("▶️  Executing workflow: {}", workflow.name);
    println!("   Steps: {}", workflow.steps.len());

    let context = WorkflowContext::new(workflow.id, registry.clone());

    // Set initial parameters
    if let Some(params_json) = params {
        let params: serde_json::Value = serde_json::from_str(&params_json)?;
        for (key, value) in params.as_object().unwrap_or(&serde_json::Map::new()) {
            context.set_variable(key.clone(), value.clone());
        }
    }

    let executor = DefaultWorkflowExecutor::new(registry);
    let start = std::time::Instant::now();

    match executor.execute(&workflow, &context).await {
        Ok(result) => {
            let elapsed = start.elapsed();
            println!("✅ Workflow completed in {:.2}s", elapsed.as_secs_f64());
            println!("   Result: {}", serde_json::to_string_pretty(&result)?);
        }
        Err(e) => {
            println!("❌ Workflow failed: {}", e);
            std::process::exit(1);
        }
    }

    Ok(())
}

#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
async fn validate_workflow(file: String) -> Result<(), Box<dyn std::error::Error>> {
    println!("🔍 Validating workflow: {}", file);

    let content = fs::read_to_string(&file).await?;

    match DslCompiler::compile(&content) {
        Ok(workflow) => {
            println!("✅ Valid workflow: {}", workflow.name);
            println!("   Version: {}", workflow.version);
            println!("   Steps: {}", workflow.steps.len());

            for (i, step) in workflow.steps.iter().enumerate() {
                println!("   {}. {} ({})", i + 1, step.name, step.id);
            }
        }
        Err(e) => {
            println!("❌ Invalid workflow: {}", e);
            std::process::exit(1);
        }
    }

    Ok(())
}

#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
async fn analyze_code(
    _registry: Arc<AgentRegistry>,
    path: String,
    language: String,
    output: String,
) -> Result<(), Box<dyn std::error::Error>> {
    use pmat::quality::complexity::ComplexityAnalyzer;
    use pmat::quality::entropy::EntropyCalculator;
    use pmat::quality::satd_item::SatdDetectorWithItems;

    println!("🔬 Analyzing: {}", path);

    let code = fs::read_to_string(&path).await?;

    // Run analyzers
    let analyzer = ComplexityAnalyzer::default();
    let complexity = analyzer.analyze_string(&code).unwrap_or_default();

    let detector = SatdDetectorWithItems::new();
    let satd_items = detector.detect(&code);

    let calculator = EntropyCalculator::new();
    let entropy = calculator.calculate(&code);

    match output.as_str() {
        "json" => {
            let result = serde_json::json!({
                "file": path,
                "language": language,
                "complexity": {
                    "cyclomatic": complexity.cyclomatic,
                    "cognitive": complexity.cognitive,
                },
                "satd": satd_items,
                "entropy": entropy,
            });
            println!("{}", serde_json::to_string_pretty(&result)?);
        }
        "html" => {
            // Would generate HTML report
            println!("HTML output not yet implemented");
        }
        _ => {
            // Text output
            println!("📊 Analysis Results:");
            println!("   Cyclomatic Complexity: {}", complexity.cyclomatic);
            println!("   Cognitive Complexity: {}", complexity.cognitive);
            println!("   Shannon Entropy: {:.2}", entropy);
            println!("   SATD Items: {}", satd_items.len());

            if !satd_items.is_empty() {
                println!("\n⚠️  Self-Admitted Technical Debt:");
                for item in satd_items {
                    println!(
                        "   - {} (line {}): {}",
                        item.satd_type, item.line, item.comment
                    );
                }
            }
        }
    }

    Ok(())
}

#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
async fn run_quality_gate(
    _registry: Arc<AgentRegistry>,
    path: String,
    language: String,
    max_complexity: u32,
    max_satd: usize,
    fail_on_violation: bool,
) -> Result<(), Box<dyn std::error::Error>> {
    use pmat::quality::complexity::ComplexityAnalyzer;
    use pmat::quality::gate_runner::{QualityGateRunner, QualityThresholds};
    use pmat::quality::satd_item::SatdDetectorWithItems;

    println!("🚦 Running quality gates on: {}", path);

    let code = fs::read_to_string(&path).await?;

    let gate = QualityGateRunner::new(
        vec![
            Box::new(ComplexityAnalyzer::default()),
            Box::new(SatdDetectorWithItems::new()),
        ],
        QualityThresholds {
            max_complexity,
            max_satd_items: max_satd,
            min_test_coverage: 0.0, // Not checking coverage
            max_duplication: 1.0,   // Not checking duplication
        },
    );

    let result = gate.check(&code, &language).await;

    if result.passed {
        println!("✅ Quality gates PASSED");
    } else {
        println!("❌ Quality gates FAILED");

        for violation in &result.violations {
            println!("   ⚠️  {}", violation);
        }

        if fail_on_violation {
            std::process::exit(1);
        }
    }

    println!("\n📊 Metrics:");
    for (key, value) in &result.metrics {
        println!("   {}: {}", key, value);
    }

    Ok(())
}

#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
async fn show_info() -> Result<(), Box<dyn std::error::Error>> {
    println!("PMAT Agent System v{}", env!("CARGO_PKG_VERSION"));
    println!("═══════════════════════════════════════");
    println!("MCP Protocol: {}", pmat::mcp_integration::MCP_VERSION);
    println!(
        "Build: {} {}",
        env!("CARGO_PKG_NAME"),
        env!("CARGO_PKG_VERSION")
    );
    println!();
    println!("Features:");
    println!("  ✓ Actix Actor System");
    println!("  ✓ Zero-copy Message Passing");
    println!("  ✓ Event Sourcing with Snapshots");
    println!("  ✓ Raft Consensus");
    println!("  ✓ Resource Control (CPU/Memory/GPU/Network/IO)");
    println!("  ✓ MCP Protocol Integration");
    println!("  ✓ Workflow Orchestration");
    println!("  ✓ Quality Gates (Complexity/SATD/Entropy)");
    println!();
    println!("Repository: https://github.com/paiml/pmat-agent-toolkit");

    Ok(())
}