mati 0.1.2

An enforcement layer for codebase knowledge: confirmed gotchas gate what AI agents read and edit at the hook level. Not a passive memory store.
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
use std::io::IsTerminal;
use std::time::{SystemTime, UNIX_EPOCH};

use anyhow::Result;
use clap::Args;
use serde::{Deserialize, Serialize};

use mati_core::store::{
    Category, ConfidenceScore, FileRecord, GotchaRecord, Priority, QualityScore, QualityTier,
    Record, RecordLifecycle, RecordSource, RecordVersion, StalenessScore,
};

use super::colors;
use super::daemon::{daemon_result, mati_root_for, read_pid_file, DaemonResult};
use super::proxy::StoreProxy;

#[derive(Args)]
pub struct StatusArgs {}

/// Stable cache key for the status snapshot (write-seq invalidated).
const SNAPSHOT_KEY: &str = "analytics:status_cache";

/// Maximum age of a cached snapshot even if write-seq matches.
const SNAPSHOT_MAX_AGE_SECS: u64 = 86_400;

/// Snapshot payload written to `analytics:status_cache`.
#[derive(Serialize, Deserialize)]
struct StatusSnapshot {
    // Record counts
    files_count: usize,
    gotchas_count: usize,
    decisions_count: usize,
    notes_count: usize,
    deps_count: usize,

    // Confirmed gotchas
    confirmed_count: usize,

    // Quality distribution (gotchas + decisions + notes)
    excellent: u32,
    good: u32,
    acceptable: u32,
    poor: u32,
    suppressed: u32,
    quality_total: usize,

    // Confidence (files + gotchas + decisions + notes)
    avg_confidence: f32,
    median_confidence: f32,
    has_confidence: bool,

    // Hotspots
    hotspot_count: usize,

    // Cache metadata
    write_seq: u64,
    computed_at: u64,
}

/// Daemon health state for display purposes.
enum DaemonHealth {
    Running { pid: Option<u32> },
    Unresponsive { pid: Option<u32> },
    NotRunning,
}

/// Check daemon health without opening the store (reuses daemon socket logic).
async fn check_daemon_health(cwd: &std::path::Path) -> DaemonHealth {
    let root = match mati_root_for(cwd) {
        Ok(r) => r,
        Err(_) => return DaemonHealth::NotRunning,
    };

    match daemon_result(&root, "ping", serde_json::json!({})).await {
        DaemonResult::Ok(resp) if resp.get("ok") == Some(&serde_json::Value::Bool(true)) => {
            let pid = read_pid_file(&root).map(|(pid, _)| pid);
            DaemonHealth::Running { pid }
        }
        DaemonResult::Unresponsive => {
            let pid = read_pid_file(&root).map(|(pid, _)| pid);
            DaemonHealth::Unresponsive { pid }
        }
        _ => DaemonHealth::NotRunning,
    }
}

/// Print the Runtime section showing daemon health.
#[allow(clippy::too_many_arguments)]
fn print_runtime_section(
    health: &DaemonHealth,
    red: &str,
    green: &str,
    yellow: &str,
    gray: &str,
    _white: &str,
    blue: &str,
    reset: &str,
) {
    println!("  {blue}Runtime{reset}");
    match health {
        DaemonHealth::Running { pid } => {
            let pid_str = pid.map(|p| format!(" (pid {p})")).unwrap_or_default();
            println!("    Daemon               {green}running{reset}{pid_str}");
            println!("    Enforcement          {green}active{reset}");
        }
        DaemonHealth::Unresponsive { pid } => {
            let pid_str = pid.map(|p| format!(" (pid {p})")).unwrap_or_default();
            println!("    Daemon               {yellow}unresponsive{reset}{pid_str}");
            println!(
                "    Enforcement          {yellow}degraded{reset} {gray}— hooks fail open{reset}"
            );
        }
        DaemonHealth::NotRunning => {
            println!("    Daemon               {red}not running{reset}");
            println!(
                "    Enforcement          {red}degraded{reset} {gray}— hooks fail open without daemon{reset}"
            );
        }
    }
    println!();
}

fn now_secs() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs()
}

fn daily_agg_count(record: Option<Record>) -> u64 {
    record
        .and_then(|r| r.payload_as::<mati_core::store::session::DailyAgg>())
        .map(|agg| agg.count)
        .unwrap_or(0)
}

#[derive(Default)]
struct CodexDailyMetrics {
    bootstrap_count: u64,
    shell_miss_count: u64,
    prompt_nudge_count: u64,
}

impl CodexDailyMetrics {
    fn has_activity(&self) -> bool {
        self.bootstrap_count > 0 || self.shell_miss_count > 0 || self.prompt_nudge_count > 0
    }
}

async fn load_codex_daily_metrics(store: &StoreProxy) -> Result<CodexDailyMetrics> {
    Ok(CodexDailyMetrics {
        bootstrap_count: daily_agg_count(
            store
                .get(&mati_core::store::session::today_key(
                    "analytics:bootstrap_",
                ))
                .await?,
        ),
        shell_miss_count: daily_agg_count(
            store
                .get(&mati_core::store::session::today_key(
                    "compliance:codex_shell_miss_",
                ))
                .await?,
        ),
        prompt_nudge_count: daily_agg_count(
            store
                .get(&mati_core::store::session::today_key(
                    "analytics:codex_prompt_nudge_",
                ))
                .await?,
        ),
    })
}

/// Count of `AllowAfterReceipt` enforcement events today.
///
/// Platform-neutral: bumped when any agent (Claude pre-read, Codex post-bash)
/// reads a file after presenting a valid consultation receipt.
async fn load_allow_after_receipt_count(store: &StoreProxy) -> Result<u64> {
    Ok(daily_agg_count(
        store
            .get(&mati_core::store::session::today_key(
                "compliance:allow_after_receipt_",
            ))
            .await?,
    ))
}

pub async fn run(_args: StatusArgs) -> Result<()> {
    let cwd = std::env::current_dir()?;
    let store = StoreProxy::open(&cwd).await?;
    let claude_mode = cwd.join(".claude/settings.json").exists();
    let codex_mode = cwd.join(".codex/config.toml").exists();
    let codex_metrics = if codex_mode {
        Some(load_codex_daily_metrics(&store).await?)
    } else {
        None
    };
    // Platform-neutral: load whenever any agent is active.
    let allow_after_receipt_count = if claude_mode || codex_mode {
        load_allow_after_receipt_count(&store).await?
    } else {
        0
    };

    // ── Daemon health (real-time, never cached) ──────────────────────────
    let daemon_health = check_daemon_health(&cwd).await;

    // ── Load cluster index (shared by cached and live paths) ───────────────
    let cluster_index: Option<mati_core::analysis::clusters::ClusterIndex> = store
        .get("cluster:index")
        .await
        .ok()
        .flatten()
        .and_then(|r| r.payload_as::<mati_core::analysis::clusters::ClusterIndex>());

    // ── Cache check: reuse snapshot when write-seq unchanged ──────────────
    let now = now_secs();
    let current_seq = store.read_write_seq();
    match store.get(SNAPSHOT_KEY).await {
        Ok(Some(cached)) => {
            if let Some(snap) = cached.payload_as::<StatusSnapshot>() {
                let age = now.saturating_sub(snap.computed_at);
                if snap.write_seq == current_seq && age < SNAPSHOT_MAX_AGE_SECS {
                    display_cached_status(
                        &snap,
                        age,
                        &cwd,
                        claude_mode,
                        codex_mode,
                        codex_metrics.as_ref(),
                        allow_after_receipt_count,
                        &daemon_health,
                        cluster_index.as_ref(),
                    );
                    store.close().await?;
                    return Ok(());
                }
            }
        }
        Err(_) => {
            // Stale or corrupt cache record (e.g. pre-MessagePack migration).
            // Fall through to recompute — the new write will overwrite the stale entry.
        }
        Ok(None) => {}
    }

    let use_color = std::io::stdout().is_terminal();

    let (red, blue, green, yellow, gray, white, bold, reset) = if use_color {
        (
            colors::RED,
            colors::BLUE,
            colors::GREEN,
            colors::YELLOW,
            colors::GRAY,
            colors::WHITE,
            colors::BOLD,
            colors::RESET,
        )
    } else {
        ("", "", "", "", "", "", "", "")
    };

    // ── Scan all namespaces in parallel ───────────────────────────────────
    let (mut files, mut gotchas, mut decisions, mut notes, mut deps) = tokio::try_join!(
        store.scan_prefix("file:"),
        store.scan_prefix("gotcha:"),
        store.scan_prefix("decision:"),
        store.scan_prefix("dev_note:"),
        store.scan_prefix("dep:"),
    )?;

    // Count only Active records. Tombstoned/superseded records must not inflate
    // the dashboard counts — `mati ls` already filters this way, but `status`
    // used to count every lifecycle, over-reporting totals and the confirmed
    // count (notably confirmed-then-tombstoned gotchas). Filtering at the source
    // fixes both the live render below and the StatusSnapshot cache built from
    // these vecs.
    files.retain(|r| matches!(r.lifecycle, RecordLifecycle::Active));
    gotchas.retain(|r| matches!(r.lifecycle, RecordLifecycle::Active));
    decisions.retain(|r| matches!(r.lifecycle, RecordLifecycle::Active));
    notes.retain(|r| matches!(r.lifecycle, RecordLifecycle::Active));
    deps.retain(|r| matches!(r.lifecycle, RecordLifecycle::Active));

    // ── Project name from cwd ─────────────────────────────────────────────
    let project = cwd
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or("unknown");

    println!("\n{bold}{blue}◈ mati status{reset} — project: {bold}{white}{project}{reset}\n");

    // ── Runtime (daemon health) ──────────────────────────────────────────
    if claude_mode || codex_mode {
        print_runtime_section(&daemon_health, red, green, yellow, gray, white, blue, reset);
    }

    if claude_mode || codex_mode {
        println!("  {blue}Platform{reset}");
        if claude_mode {
            println!("    Claude  — hard read enforcement");
        }
        if codex_mode {
            println!("    Codex   — hard Bash enforcement, soft native-read enforcement");
        }
        println!();
    }

    // ── Integrity warning ────────────────────────────────────────────────
    if let Ok(Some(marker_record)) = store.get(mati_core::store::repair::DIRTY_MARKER_KEY).await {
        if let Some(marker) = marker_record.payload_as::<mati_core::store::repair::DirtyMarker>() {
            if marker.dirty {
                println!(
                    "  {yellow}⚠ Index drift detected{reset}{gray}{}{reset}",
                    marker.cause
                );
                println!(
                    "    {gray}Affected keys: {}. Run `mati repair` to reconcile.{reset}\n",
                    marker.affected_keys.len()
                );
            }
        }
    }

    if allow_after_receipt_count > 0 {
        println!(
            "  {blue}Compliance Today{reset}  allow after receipt {white}{}{reset}\n",
            allow_after_receipt_count,
        );
    }

    if let Some(metrics) = codex_metrics.as_ref().filter(|m| m.has_activity()) {
        println!(
            "  {blue}Codex Today{reset}  bootstraps {white}{}{reset}  shell misses {white}{}{reset}  prompt nudges {white}{}{reset}\n",
            metrics.bootstrap_count,
            metrics.shell_miss_count,
            metrics.prompt_nudge_count,
        );
    }

    // ── Record counts ─────────────────────────────────────────────────────
    println!(
        "  {blue}Records{reset}     {white}{}{reset} files  {white}{}{reset} gotchas  {white}{}{reset} decisions  {white}{}{reset} notes  {white}{}{reset} deps",
        files.len(),
        gotchas.len(),
        decisions.len(),
        notes.len(),
        deps.len(),
    );

    // ── Confirmed count ───────────────────────────────────────────────────
    let confirmed_count = gotchas
        .iter()
        .filter(|r| {
            r.payload_as::<GotchaRecord>()
                .map(|gr| gr.confirmed)
                .unwrap_or(false)
        })
        .count();

    let total_gotchas = gotchas.len();
    let pct = if total_gotchas > 0 {
        (confirmed_count as f32 / total_gotchas as f32 * 100.0) as u32
    } else {
        0
    };
    println!(
        "  {blue}Confirmed{reset}    {green}{confirmed_count}{reset} / {total_gotchas} gotchas ({pct}%)"
    );

    // ── Quality distribution ──────────────────────────────────────────────
    let quality_records: Vec<&Record> = gotchas
        .iter()
        .chain(decisions.iter())
        .chain(notes.iter())
        .collect();

    let (mut excellent, mut good, mut acceptable, mut poor, mut suppressed) =
        (0u32, 0u32, 0u32, 0u32, 0u32);

    if !quality_records.is_empty() {
        for r in &quality_records {
            match r.quality.tier {
                QualityTier::Excellent => excellent += 1,
                QualityTier::Good => good += 1,
                QualityTier::Acceptable => acceptable += 1,
                QualityTier::Poor => poor += 1,
                QualityTier::Suppressed => suppressed += 1,
            }
        }

        let total = quality_records.len() as f32;
        println!("\n  {blue}Quality Distribution{reset}");
        print_bar("Excellent", excellent, total, green, white, reset);
        print_bar("Good", good, total, green, white, reset);
        print_bar("Acceptable", acceptable, total, yellow, white, reset);
        print_bar("Poor", poor, total, yellow, white, reset);
        print_bar("Suppressed", suppressed, total, gray, white, reset);
    }

    // ── Confidence summary ────────────────────────────────────────────────
    let all_knowledge: Vec<&Record> = files
        .iter()
        .chain(gotchas.iter())
        .chain(decisions.iter())
        .chain(notes.iter())
        .collect();

    let (avg_confidence, median_confidence, has_confidence) = if !all_knowledge.is_empty() {
        let mut conf_values: Vec<f32> = all_knowledge.iter().map(|r| r.confidence.value).collect();
        conf_values.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
        let avg = conf_values.iter().sum::<f32>() / conf_values.len() as f32;
        let n = conf_values.len();
        let median = if n % 2 == 0 {
            (conf_values[n / 2 - 1] + conf_values[n / 2]) / 2.0
        } else {
            conf_values[n / 2]
        };
        println!(
            "\n  {blue}Confidence{reset}   avg {white}{avg:.2}{reset}  median {white}{median:.2}{reset}"
        );
        (avg, median, true)
    } else {
        (0.0, 0.0, false)
    };

    // ── Hotspots ──────────────────────────────────────────────────────────
    let hotspot_count = files
        .iter()
        .filter(|r| {
            r.payload_as::<FileRecord>()
                .map(|fr| fr.is_hotspot)
                .unwrap_or(false)
        })
        .count();

    let total_files = files.len();
    let hot_pct = if total_files > 0 {
        (hotspot_count as f32 / total_files as f32 * 100.0) as u32
    } else {
        0
    };
    println!(
        "  {blue}Hotspots{reset}     {white}{hotspot_count}{reset} / {total_files} ({hot_pct}%)"
    );

    // ── Co-change clusters (from preloaded cluster index) ──────────────
    if let Some(ci) = &cluster_index {
        if ci.total > 0 {
            let largest = ci
                .clusters
                .first()
                .map(|c| format!("{} ({} files, cohesion {:.2})", c.label, c.size, c.cohesion))
                .unwrap_or_default();
            let cl_pct = if total_files > 0 {
                (ci.clustered_files as f32 / total_files as f32 * 100.0) as u32
            } else {
                0
            };
            println!(
                "\n  {blue}Clusters{reset}     {white}{}{reset} co-change clusters",
                ci.total
            );
            println!("    Largest:         {white}{largest}{reset}");
            println!(
                "    Clustered files: {white}{}{reset} / {total_files} ({cl_pct}%)",
                ci.clustered_files
            );
        }
    }

    // ── Trust health ─────────────────────────────────────────────────────
    let stale_count = gotchas
        .iter()
        .chain(decisions.iter())
        .filter(|r| {
            matches!(
                r.staleness.tier,
                mati_core::store::StalenessTier::Stale
                    | mati_core::store::StalenessTier::Liability
                    | mati_core::store::StalenessTier::Tombstone
            )
        })
        .count();
    let low_confidence_count = gotchas
        .iter()
        .chain(decisions.iter())
        .filter(|r| r.confidence.value < 0.3)
        .count();

    let unconfirmed = total_gotchas - confirmed_count;

    // Compute oldest unconfirmed age for backlog visibility
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();
    let oldest_unconfirmed_days = gotchas
        .iter()
        .filter(|r| {
            r.payload_as::<GotchaRecord>()
                .map(|g| !g.confirmed)
                .unwrap_or(false)
        })
        .map(|r| (now.saturating_sub(r.created_at)) / 86400)
        .max()
        .unwrap_or(0);

    // Count unconfirmed candidates on hotspot files
    let unconfirmed_on_hotspots = gotchas
        .iter()
        .filter(|r| {
            r.payload_as::<GotchaRecord>()
                .map(|g| !g.confirmed)
                .unwrap_or(false)
        })
        .filter(|r| {
            r.payload_as::<GotchaRecord>()
                .map(|g| {
                    g.affected_files.iter().any(|af| {
                        files.iter().any(|f| {
                            f.key == format!("file:{af}")
                                && f.payload_as::<FileRecord>()
                                    .map(|fr| fr.is_hotspot)
                                    .unwrap_or(false)
                        })
                    })
                })
                .unwrap_or(false)
        })
        .count();

    let has_trust_issues = stale_count > 0 || low_confidence_count > 0 || unconfirmed > 0;

    if has_trust_issues {
        println!("\n  {blue}Trust{reset}");
        if unconfirmed > 0 {
            let mut detail_parts = Vec::new();
            if unconfirmed_on_hotspots > 0 {
                detail_parts.push(format!("{unconfirmed_on_hotspots} on hotspots"));
            }
            if oldest_unconfirmed_days > 0 {
                detail_parts.push(format!("oldest {oldest_unconfirmed_days}d"));
            }
            let detail = if detail_parts.is_empty() {
                String::new()
            } else {
                format!(" {gray}({}){reset}", detail_parts.join(", "))
            };
            println!(
                "    {yellow}{unconfirmed} unconfirmed{reset}{detail} {gray}— run `mati review`{reset}"
            );
        }
        if stale_count > 0 {
            println!(
                "    {yellow}{stale_count} stale{reset} {gray}— run `mati stale` to see which records need attention{reset}"
            );
        }
        if low_confidence_count > 0 {
            println!(
                "    {yellow}{low_confidence_count} low-confidence{reset} {gray}(<0.3) — may need enrichment or manual review{reset}"
            );
        }
    }

    // ── Workflow guidance ────────────────────────────────────────────────
    if total_gotchas == 0 {
        println!();
        println!("  {gray}No gotchas yet. Run `mati init` to scan for candidates.{reset}");
    }

    println!();

    // ── Write snapshot (best-effort) ──────────────────────────────────────
    let snap = StatusSnapshot {
        files_count: files.len(),
        gotchas_count: gotchas.len(),
        decisions_count: decisions.len(),
        notes_count: notes.len(),
        deps_count: deps.len(),
        confirmed_count,
        excellent,
        good,
        acceptable,
        poor,
        suppressed,
        quality_total: quality_records.len(),
        avg_confidence,
        median_confidence,
        has_confidence,
        hotspot_count,
        write_seq: current_seq,
        computed_at: now,
    };
    let _ = write_snapshot_record(&store, &snap, now).await;

    store.close().await?;
    Ok(())
}

/// Write a `StatusSnapshot` to `SNAPSHOT_KEY` via proxy.
async fn write_snapshot_record(store: &StoreProxy, snap: &StatusSnapshot, now: u64) -> Result<()> {
    let record = Record {
        key: SNAPSHOT_KEY.to_string(),
        value: String::new(),
        payload: serde_json::to_value(snap).ok(),
        category: Category::Analytics,
        priority: Priority::Normal,
        tags: vec![],
        created_at: now,
        updated_at: now,
        ref_url: None,
        staleness: StalenessScore::fresh(),
        lifecycle: RecordLifecycle::Active,
        version: RecordVersion {
            device_id: mati_core::store::stable_device_id(),
            logical_clock: 1,
            wall_clock: now,
        },
        quality: QualityScore::layer0_default(),
        access_count: 0,
        last_accessed: 0,
        source: RecordSource::StaticAnalysis,
        confidence: ConfidenceScore::for_new_record(&RecordSource::StaticAnalysis),
        gap_analysis_score: 0.0,
    };
    store.put(SNAPSHOT_KEY, &record).await
}

/// Render status output from a cached snapshot.
#[allow(clippy::too_many_arguments)]
fn display_cached_status(
    s: &StatusSnapshot,
    age: u64,
    cwd: &std::path::Path,
    claude_mode: bool,
    codex_mode: bool,
    codex_metrics: Option<&CodexDailyMetrics>,
    allow_after_receipt_count: u64,
    daemon_health: &DaemonHealth,
    cluster_index: Option<&mati_core::analysis::clusters::ClusterIndex>,
) {
    let use_color = std::io::stdout().is_terminal();

    let (red, blue, green, yellow, gray, white, bold, reset) = if use_color {
        (
            colors::RED,
            colors::BLUE,
            colors::GREEN,
            colors::YELLOW,
            colors::GRAY,
            colors::WHITE,
            colors::BOLD,
            colors::RESET,
        )
    } else {
        ("", "", "", "", "", "", "", "")
    };

    let project = cwd
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or("unknown");

    println!(
        "\n{bold}{blue}◈ mati status{reset} — project: {bold}{white}{project}{reset}  {gray}(cached {}s ago){reset}\n",
        age
    );

    // ── Runtime (daemon health — always real-time, never cached) ─────────
    if claude_mode || codex_mode {
        print_runtime_section(daemon_health, red, green, yellow, gray, white, blue, reset);
    }

    if claude_mode || codex_mode {
        println!("  {blue}Platform{reset}");
        if claude_mode {
            println!("    Claude  — hard read enforcement");
        }
        if codex_mode {
            println!("    Codex   — hard Bash enforcement, soft native-read enforcement");
        }
        println!();
    }

    if allow_after_receipt_count > 0 {
        println!(
            "  {blue}Compliance Today{reset}  allow after receipt {white}{}{reset}\n",
            allow_after_receipt_count,
        );
    }

    if let Some(metrics) = codex_metrics.filter(|m| m.has_activity()) {
        println!(
            "  {blue}Codex Today{reset}  bootstraps {white}{}{reset}  shell misses {white}{}{reset}  prompt nudges {white}{}{reset}\n",
            metrics.bootstrap_count,
            metrics.shell_miss_count,
            metrics.prompt_nudge_count,
        );
    }

    println!(
        "  {blue}Records{reset}     {white}{}{reset} files  {white}{}{reset} gotchas  {white}{}{reset} decisions  {white}{}{reset} notes  {white}{}{reset} deps",
        s.files_count, s.gotchas_count, s.decisions_count, s.notes_count, s.deps_count,
    );

    let pct = if s.gotchas_count > 0 {
        (s.confirmed_count as f32 / s.gotchas_count as f32 * 100.0) as u32
    } else {
        0
    };
    println!(
        "  {blue}Confirmed{reset}    {green}{}{reset} / {} gotchas ({pct}%)",
        s.confirmed_count, s.gotchas_count,
    );

    if s.quality_total > 0 {
        let total = s.quality_total as f32;
        println!("\n  {blue}Quality Distribution{reset}");
        print_bar("Excellent", s.excellent, total, green, white, reset);
        print_bar("Good", s.good, total, green, white, reset);
        print_bar("Acceptable", s.acceptable, total, yellow, white, reset);
        print_bar("Poor", s.poor, total, yellow, white, reset);
        print_bar("Suppressed", s.suppressed, total, gray, white, reset);
    }

    if s.has_confidence {
        println!(
            "\n  {blue}Confidence{reset}   avg {white}{:.2}{reset}  median {white}{:.2}{reset}",
            s.avg_confidence, s.median_confidence,
        );
    }

    let hot_pct = if s.files_count > 0 {
        (s.hotspot_count as f32 / s.files_count as f32 * 100.0) as u32
    } else {
        0
    };
    println!(
        "  {blue}Hotspots{reset}     {white}{}{reset} / {} ({hot_pct}%)",
        s.hotspot_count, s.files_count,
    );

    // ── Co-change clusters (from preloaded cluster index) ───────────────
    if let Some(ci) = cluster_index {
        if ci.total > 0 {
            let largest = ci
                .clusters
                .first()
                .map(|c| format!("{} ({} files, cohesion {:.2})", c.label, c.size, c.cohesion))
                .unwrap_or_default();
            let cl_pct = if s.files_count > 0 {
                (ci.clustered_files as f32 / s.files_count as f32 * 100.0) as u32
            } else {
                0
            };
            println!(
                "\n  {blue}Clusters{reset}     {white}{}{reset} co-change clusters",
                ci.total
            );
            println!("    Largest:         {white}{largest}{reset}");
            println!(
                "    Clustered files: {white}{}{reset} / {} ({cl_pct}%)",
                ci.clustered_files, s.files_count
            );
        }
    }

    println!();
}

fn print_bar(label: &str, count: u32, total: f32, color: &str, white: &str, reset: &str) {
    if count == 0 {
        return;
    }
    let pct = (count as f32 / total * 100.0) as u32;
    let bar_width = (count as f32 / total * 20.0).ceil() as usize;
    let bar: String = "".repeat(bar_width);
    println!(
        "    {:<12} {white}{:>3}{reset}  {color}{bar}{reset}  {pct}%",
        label, count
    );
}