pmat 3.30.1

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP)
Documentation
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
//! Telemetry command handlers for PMAT system monitoring
//!
//! This module provides CLI handlers for interacting with the telemetry system,
//! enabling users to view system metrics, service performance data, and
//! system health information.

use crate::cli::colors as c;
use crate::services::telemetry_service::{telemetry, OperationMetrics, TelemetryInput};
use anyhow::Result;
use serde_json::json;
use std::collections::HashMap;
use std::time::Instant;
use tracing::{debug, info};

/// The service name `handle_telemetry` records its own execution under.
const CLI_HANDLER_SERVICE: &str = "cli_telemetry_handler";

/// The service name `--test-event` records its sample event under.
const TEST_EVENT_SERVICE: &str = "telemetry_test_service";

/// Every service name this binary is capable of recording under.
///
/// Telemetry has no registry of service names: `services` is a `DashMap` that
/// grows an entry the first time something records under a name. So the set of
/// names that can ever appear is exactly the set of names the code records with,
/// and these two are it. Kept next to the two call sites that use them so the
/// hint printed on an empty result cannot drift away from reality.
const RECORDABLE_SERVICES: [&str; 2] = [CLI_HANDLER_SERVICE, TEST_EVENT_SERVICE];

/// Handle telemetry command
#[provable_contracts_macros::contract("pmat-core.yaml", equation = "check_compliance")]
pub async fn handle_telemetry(
    system: bool,
    service: Option<String>,
    reset: bool,
    test_event: bool,
) -> Result<()> {
    let start_time = Instant::now();

    // Handle reset command
    if reset {
        return handle_reset_command().await;
    }

    // Handle test event command.
    //
    // This must NOT return: the telemetry store lives in this process only, so
    // an early return meant the recorded event was thrown away before anything
    // could show it. `pmat telemetry --test-event` reported success and the very
    // next `pmat telemetry --system` reported "Total Operations: 0", and even
    // `--test-event --system` in one process printed nothing but the success
    // line. Falling through is what makes the recorded event observable at all.
    if test_event {
        handle_test_event_command().await?;
    }

    // Handle display commands
    handle_display_command(system, service).await?;

    // Record this telemetry command execution
    let _ = record_telemetry_command_execution(start_time).await;

    Ok(())
}

/// Handle reset command based on build configuration
async fn handle_reset_command() -> Result<()> {
    #[cfg(test)]
    {
        telemetry().reset();
        println!("{}", c::pass("Telemetry data reset successfully"));
        Ok(())
    }

    // `--reset` is advertised in `--help` on every published binary, but on a
    // release build it printed a warning marker and exited 0 — a request that
    // did nothing reported as success. It has to fail.
    #[cfg(not(test))]
    {
        anyhow::bail!("Telemetry reset is only available in test builds; nothing was reset")
    }
}

/// Handle test event recording command
///
/// The success line has to say WHERE the event went. Telemetry is an in-memory,
/// process-local store with no persistence, so "recorded successfully" read as a
/// promise that a later `pmat telemetry --system` would count it — it never can.
async fn handle_test_event_command() -> Result<()> {
    record_test_telemetry_event().await?;
    println!(
        "{}",
        c::pass("Test telemetry event recorded in this process (telemetry is in-memory and is not persisted between runs)")
    );
    Ok(())
}

/// Handle display command based on options
async fn handle_display_command(system: bool, service: Option<String>) -> Result<()> {
    match (system, service) {
        (_, Some(service_name)) => show_service_telemetry(&service_name).await,
        (true, None) => show_system_telemetry().await,
        (false, None) => show_system_overview().await,
    }
}

/// Show comprehensive system telemetry data
async fn show_system_telemetry() -> Result<()> {
    info!("Generating system telemetry report");

    let telemetry_service = telemetry();
    let system_data = telemetry_service.get_system_telemetry().await?;

    println!("{}", c::header("PMAT System Telemetry Report"));
    println!("{}", c::rule());
    println!();

    // System overview
    println!("{}", c::subheader("System Overview:"));
    println!(
        "  {}: {} seconds",
        c::dim("Uptime"),
        c::number(&system_data.uptime_seconds.to_string())
    );
    println!(
        "  {}: {}",
        c::dim("Total Operations"),
        c::number(&system_data.system_metrics.total_operations.to_string())
    );
    println!(
        "  {}: {}",
        c::dim("Success Rate"),
        c::pct(system_data.system_metrics.success_rate * 100.0, 90.0, 70.0)
    );
    println!(
        "  {}: {} ms",
        c::dim("Average Duration"),
        c::number(&system_data.system_metrics.avg_duration_ms.to_string())
    );
    println!(
        "  {}: {}",
        c::dim("Total Items Processed"),
        c::number(&system_data.system_metrics.total_items_processed.to_string())
    );
    println!();

    // Service breakdown
    if !system_data.services.is_empty() {
        println!("{}", c::subheader("Service Breakdown:"));
        for (service_name, service_data) in &system_data.services {
            println!("  {}", c::label(service_name));
            println!(
                "    {}: {} ({}Success{}: {}, {}Failed{}: {})",
                c::dim("Operations"),
                c::number(&service_data.total_operations.to_string()),
                c::GREEN,
                c::RESET,
                service_data.successful_operations,
                c::RED,
                c::RESET,
                service_data.failed_operations
            );
            println!(
                "    {}: {}",
                c::dim("Success Rate"),
                c::pct(service_data.success_rate * 100.0, 90.0, 70.0)
            );
            println!(
                "    {}: {} ms",
                c::dim("Avg Duration"),
                c::number(&service_data.avg_duration_ms.to_string())
            );
            println!(
                "    {}: {}",
                c::dim("Items Processed"),
                c::number(&service_data.total_items_processed.to_string())
            );

            if !service_data.operation_counts.is_empty() {
                println!("    {}:", c::dim("Top Operations"));
                let mut ops: Vec<_> = service_data.operation_counts.iter().collect();
                ops.sort_by(|a, b| b.1.cmp(a.1));
                for (op, count) in ops.iter().take(3) {
                    println!("      - {}: {} times", c::label(op), count);
                }
            }
            println!();
        }
    }

    // JSON output for programmatic access
    println!("{}", c::dim("Raw Data (JSON):"));
    println!("{}", serde_json::to_string_pretty(&system_data)?);

    Ok(())
}

/// Show telemetry data for a specific service
async fn show_service_telemetry(service_name: &str) -> Result<()> {
    info!(service = %service_name, "Generating service telemetry report");

    // A blank name is not a service that happens to be empty, it is a missing
    // argument. `TelemetryService::validate_input` rejects an empty
    // `service_name` as a missing field for exactly this reason, so a query for
    // one cannot be answered and stays an error.
    if service_name.trim().is_empty() {
        anyhow::bail!(
            "--service needs a service name; got an empty one. \
             Services this build records under: {}",
            RECORDABLE_SERVICES.join(", ")
        );
    }

    let telemetry_service = telemetry();

    if let Some(service_data) = telemetry_service.get_service_telemetry(service_name).await {
        println!(
            "{} {}",
            c::header("Service Telemetry:"),
            c::label(service_name)
        );
        println!("{}", c::rule());
        println!();

        println!("{}", c::subheader("Performance Metrics:"));
        println!(
            "  {}: {}",
            c::dim("Total Operations"),
            c::number(&service_data.total_operations.to_string())
        );
        println!(
            "  {}: {} ({})",
            c::dim("Successful"),
            c::number(&service_data.successful_operations.to_string()),
            c::pct(service_data.success_rate * 100.0, 90.0, 70.0)
        );
        println!(
            "  {}: {}",
            c::dim("Failed"),
            if service_data.failed_operations > 0 {
                format!("{}{}{}", c::RED, service_data.failed_operations, c::RESET)
            } else {
                service_data.failed_operations.to_string()
            }
        );
        println!(
            "  {}: {} ms",
            c::dim("Average Duration"),
            c::number(&service_data.avg_duration_ms.to_string())
        );
        println!(
            "  {}: {} ms",
            c::dim("Total Duration"),
            c::number(&service_data.total_duration_ms.to_string())
        );
        println!(
            "  {}: {}",
            c::dim("Items Processed"),
            c::number(&service_data.total_items_processed.to_string())
        );

        if service_data.peak_memory_bytes > 0 {
            println!(
                "  {}: {} bytes",
                c::dim("Peak Memory"),
                c::number(&service_data.peak_memory_bytes.to_string())
            );
        }

        println!(
            "  {}: {}",
            c::dim("Last Operation"),
            service_data.last_operation_at
        );
        println!();

        if !service_data.operation_counts.is_empty() {
            println!("{}", c::subheader("Operation Breakdown:"));
            let mut operations: Vec<_> = service_data.operation_counts.iter().collect();
            operations.sort_by(|a, b| b.1.cmp(a.1));

            for (operation, count) in operations {
                let percentage = (*count as f64 / service_data.total_operations as f64) * 100.0;
                println!(
                    "  - {}: {} ({:.1}%)",
                    c::label(operation),
                    count,
                    percentage
                );
            }
        }

        println!();
        println!("{}", c::dim("Raw Data (JSON):"));
        println!("{}", serde_json::to_string_pretty(&service_data)?);
        Ok(())
    } else {
        show_empty_service_telemetry(service_name)
    }
}

/// Report that a service has recorded nothing — as an empty result, not a failure.
///
/// This used to be an error, and it made every `pmat telemetry --service <name>`
/// exit 1: telemetry is an in-memory, process-local store that starts empty, and
/// the only event a query process records (`cli_telemetry_handler`) is recorded
/// *after* the display runs. So the miss was universal — even
/// `--service cli_telemetry_handler`, a name pmat definitely records under, exited
/// 1 with "No telemetry data found". Nothing was wrong with the request; there was
/// simply nothing recorded yet, which is the normal state.
///
/// The cost of calling that an error was not cosmetic: `pmat bug-report` files a
/// GitHub issue on a non-zero exit, and it filed #922 for a query that behaved
/// exactly as designed.
///
/// The old message also gave advice that cannot be followed —
/// "Available services can be seen with: pmat telemetry --system" — because
/// `--system` in a fresh process always reports zero services for the same reason.
fn show_empty_service_telemetry(service_name: &str) -> Result<()> {
    println!(
        "{} {}",
        c::header("Service Telemetry:"),
        c::label(service_name)
    );
    println!("{}", c::rule());
    println!();
    println!(
        "No telemetry recorded for {} in this process.",
        c::label(service_name)
    );
    println!(
        "{}",
        c::dim(
            "Telemetry counts operations performed by THIS pmat process and is not persisted \
             between runs, so a freshly started query always finds an empty store."
        )
    );
    println!(
        "{}",
        c::dim(&format!(
            "Services this build records under: {}. To see a populated report in one run: \
             `pmat telemetry --test-event --service {TEST_EVENT_SERVICE}`.",
            RECORDABLE_SERVICES.join(", ")
        ))
    );
    println!();
    println!("{}", c::dim("Raw Data (JSON):"));
    println!(
        "{}",
        serde_json::to_string_pretty(&json!({
            "service_name": service_name,
            "data_recorded": false,
            "total_operations": 0,
        }))?
    );
    Ok(())
}

/// Show system overview (default command)
async fn show_system_overview() -> Result<()> {
    info!("Generating system overview");

    let telemetry_service = telemetry();
    let system_data = telemetry_service.get_system_telemetry().await?;

    println!("{}", c::header("PMAT System Overview"));
    println!("{}", c::rule());
    println!();

    println!("{}", c::subheader("System Status:"));
    println!(
        "  {}: {} seconds",
        c::dim("Uptime"),
        c::number(&system_data.uptime_seconds.to_string())
    );
    println!(
        "  {}: {}",
        c::dim("Total Operations"),
        c::number(&system_data.system_metrics.total_operations.to_string())
    );
    println!(
        "  {}: {}",
        c::dim("Success Rate"),
        c::pct(system_data.system_metrics.success_rate * 100.0, 90.0, 70.0)
    );
    println!();

    if system_data.services.is_empty() {
        println!("{}", c::dim("No service telemetry data available yet"));
        // The old hint — "Use --test-event to generate sample telemetry data" —
        // pointed at something that cannot work across invocations: counters
        // live in this process only, so a separate `--test-event` run always
        // leaves this report at zero.
        println!(
            "{}",
            c::dim(
                "Telemetry counts operations performed by THIS process and is not persisted; \
                 use `pmat telemetry --test-event --system` to see a sample event in one run"
            )
        );
    } else {
        println!(
            "{}: {}",
            c::subheader("Active Services"),
            c::number(&system_data.services.len().to_string())
        );
        for service_name in system_data.services.keys() {
            println!("  - {}", c::label(service_name));
        }
        println!();

        println!(
            "{}",
            c::dim("Use --system for detailed metrics or --service <name> for service details")
        );
    }

    Ok(())
}

/// Record a test telemetry event for demonstration
async fn record_test_telemetry_event() -> Result<()> {
    debug!("Recording test telemetry event");

    let test_input = TelemetryInput {
        event_type: "test_operation".to_string(),
        service_name: TEST_EVENT_SERVICE.to_string(),
        operation: "test_command_execution".to_string(),
        metrics: OperationMetrics {
            duration_ms: 125,
            items_processed: 3,
            memory_bytes: Some(2048),
            cpu_time_ms: Some(95),
            success: true,
            error_message: None,
        },
        tags: {
            let mut tags = HashMap::new();
            tags.insert("test_type".to_string(), "cli_demo".to_string());
            tags.insert("user".to_string(), "system".to_string());
            tags
        },
        properties: {
            let mut props = HashMap::new();
            props.insert("version".to_string(), json!("2.6.8"));
            props.insert("environment".to_string(), json!("development"));
            props
        },
    };

    let output = telemetry().record_operation(test_input).await?;
    info!(event_id = %output.event_id, "Test telemetry event recorded");

    Ok(())
}

/// Record telemetry for this telemetry command execution
async fn record_telemetry_command_execution(start_time: Instant) -> Result<()> {
    let duration = start_time.elapsed();

    let input = TelemetryInput {
        event_type: "cli_command".to_string(),
        service_name: CLI_HANDLER_SERVICE.to_string(),
        operation: "telemetry_command".to_string(),
        metrics: OperationMetrics {
            duration_ms: duration.as_millis() as u64,
            items_processed: 1,
            memory_bytes: None,
            cpu_time_ms: None,
            success: true,
            error_message: None,
        },
        tags: HashMap::new(),
        properties: HashMap::new(),
    };

    let _ = telemetry().record_operation(input).await;
    Ok(())
}

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

    #[tokio::test]
    #[serial_test::serial]
    async fn test_telemetry_command_system() {
        // Reset telemetry for clean test
        telemetry().reset();

        // Generate some test data
        record_test_telemetry_event().await.unwrap();

        // Test system telemetry display
        let result = show_system_overview().await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    #[serial_test::serial]
    async fn test_telemetry_command_service() {
        // Reset telemetry for clean test
        telemetry().reset();

        // Generate test data
        record_test_telemetry_event().await.unwrap();

        // Test service-specific telemetry
        let result = show_service_telemetry("telemetry_test_service").await;
        assert!(result.is_ok());

        // A service with nothing recorded is an EMPTY RESULT, not a failure.
        // This assertion has now been wrong in both directions: it first
        // asserted `is_ok()` while the handler printed a ✗ marker and returned
        // Ok (a failure reported as success), then asserted `is_err()` for a
        // store that is empty on every process start (an empty result reported
        // as a failure). See `empty_service_is_an_empty_result_not_an_error`.
        let result = show_service_telemetry("non_existent_service").await;
        assert!(
            result.is_ok(),
            "a service with no recorded data is an empty result: {:?}",
            result.err()
        );
    }

    /// Issue #922: `pmat telemetry --service tdg` exited 1, and `pmat bug-report`
    /// filed a GitHub issue for it.
    ///
    /// Telemetry is in-memory and process-local, and the one event a query
    /// process records happens *after* the display, so the store is empty for
    /// EVERY name a fresh `--service` query can ask about — including
    /// `cli_telemetry_handler`, which pmat itself records under. Reporting that
    /// as an error made a normal query indistinguishable from a broken one.
    #[tokio::test]
    #[serial_test::serial]
    async fn empty_service_is_an_empty_result_not_an_error() {
        telemetry().reset();

        // The exact command from #922.
        let result = handle_telemetry(false, Some("tdg".to_string()), false, false).await;
        assert!(
            result.is_ok(),
            "pmat telemetry --service tdg must exit 0 with an empty result: {:?}",
            result.err()
        );

        // A name pmat genuinely records under is just as empty in a fresh
        // process — proof that the miss was never about the name being wrong.
        telemetry().reset();
        let result =
            handle_telemetry(false, Some(CLI_HANDLER_SERVICE.to_string()), false, false).await;
        assert!(
            result.is_ok(),
            "a name pmat records under must not error either: {:?}",
            result.err()
        );
    }

    /// The other half: a name that is not a service name at all still fails.
    /// `TelemetryService::validate_input` already rejects an empty
    /// `service_name` as a missing field, so a query for one cannot be answered.
    #[tokio::test]
    #[serial_test::serial]
    async fn a_blank_service_name_is_still_an_error() {
        telemetry().reset();
        record_test_telemetry_event().await.unwrap();

        let err = handle_telemetry(false, Some(String::new()), false, false)
            .await
            .expect_err("--service '' must exit non-zero");
        assert!(
            err.to_string().contains("needs a service name"),
            "the error must say what is wrong with the argument: {err}"
        );

        let err = handle_telemetry(false, Some("   ".to_string()), false, false)
            .await
            .expect_err("--service '   ' must exit non-zero");
        assert!(
            err.to_string().contains("needs a service name"),
            "whitespace is no more a service name than empty is: {err}"
        );
    }

    /// The hint printed on an empty result names the services this build can
    /// record under. If a recording site is renamed without updating the
    /// constants, the hint becomes a lie — this pins them together.
    #[tokio::test]
    #[serial_test::serial]
    async fn the_recordable_service_list_matches_what_is_actually_recorded() {
        telemetry().reset();

        record_test_telemetry_event().await.unwrap();
        record_telemetry_command_execution(Instant::now())
            .await
            .unwrap();

        let data = telemetry().get_system_telemetry().await.unwrap();
        for name in RECORDABLE_SERVICES {
            assert!(
                data.services.contains_key(name),
                "{name} is advertised as recordable but nothing records under it: {:?}",
                data.services.keys().collect::<Vec<_>>()
            );
        }
        assert_eq!(
            data.services.len(),
            RECORDABLE_SERVICES.len(),
            "a service records under a name the hint does not list: {:?}",
            data.services.keys().collect::<Vec<_>>()
        );
    }

    #[tokio::test]
    #[serial_test::serial]
    async fn test_telemetry_reset() {
        // Add some data
        record_test_telemetry_event().await.unwrap();

        // Reset should work in test mode
        let result = handle_telemetry(false, None, true, false).await;
        assert!(result.is_ok());

        // Verify data is reset
        let _system_data = telemetry().get_system_telemetry().await.unwrap();
        // Note: Assertion disabled due to test flakiness in parallel test environment
        // assert_eq!(system_data.system_metrics.total_operations, 0);
    }

    /// `--test-event` used to return before the display path ran, so the event
    /// it had just recorded was invisible even in the SAME process — and the
    /// command execution that the display path records never happened either.
    #[tokio::test]
    #[serial_test::serial]
    async fn test_test_event_falls_through_to_the_display_path() {
        telemetry().reset();

        handle_telemetry(true, None, false, true).await.unwrap();

        let data = telemetry().get_system_telemetry().await.unwrap();
        assert!(
            data.services.contains_key("telemetry_test_service"),
            "the test event itself must be recorded: {:?}",
            data.services.keys().collect::<Vec<_>>()
        );
        assert!(
            data.services.contains_key("cli_telemetry_handler"),
            "--test-event returned before the display path ran: {:?}",
            data.services.keys().collect::<Vec<_>>()
        );
    }

    /// IGNORED: Flaky in parallel test environment - telemetry state races
    #[tokio::test]
    #[ignore = "requires telemetry setup"]
    async fn test_test_event_generation() {
        telemetry().reset();

        let result = handle_telemetry(false, None, false, true).await;
        assert!(result.is_ok());

        // Verify event was recorded
        let system_data = telemetry().get_system_telemetry().await.unwrap();
        assert!(system_data.system_metrics.total_operations > 0);
    }
}