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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
use std::io::IsTerminal;
use std::time::{SystemTime, UNIX_EPOCH};

use anyhow::Result;
use clap::Args;
use comfy_table::{presets::UTF8_FULL_CONDENSED, Cell, Color, ContentArrangement, Table};
use serde::{Deserialize, Serialize};

use mati_core::store::{
    Category, ConfidenceScore, FileRecord, Priority, QualityScore, Record, RecordLifecycle,
    RecordSource, RecordVersion, StalenessScore, StalenessSignal, StalenessTier, Store,
};

use super::proxy::StoreProxy;

use super::colors;
use super::show::{format_date, staleness_color, truncate};

/// Effective staleness: max of local and propagated. For file records,
/// this accounts for inherited staleness from upstream imports.
fn effective_staleness(record: &Record) -> f32 {
    let local = record.staleness.value;
    let propagated = record
        .payload_as::<FileRecord>()
        .and_then(|fr| fr.propagated_staleness)
        .map(|p| p.value)
        .unwrap_or(0.0);
    local.max(propagated)
}

// ── Cache ─────────────────────────────────────────────────────────────────────

const STALE_CACHE_KEY: &str = "analytics:stale_cache";

/// Write-seq–invalidated cache of the full stale record list.
/// Records are pre-sorted by staleness value descending; apply `--limit` at display time.
#[derive(Serialize, Deserialize)]
struct StaleCache {
    /// Knowledge write-seq at cache time. Cache is valid while this matches the store.
    write_seq: u64,
    records: Vec<Record>,
}

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

fn cache_record(value: String) -> Record {
    let now = now_secs();
    Record {
        key: STALE_CACHE_KEY.to_string(),
        value,
        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,
        payload: None,
    }
}

/// Seed the stale cache from in-memory records immediately after `mati init`.
///
/// Called only on cold init (skipped_count == 0), where all records are freshly
/// written and their staleness tiers are accurate. On cold init the stale list
/// is always empty, so this writes a trivially small record that lets the very
/// first post-init `mati stale` hit the cache (O(1)).
pub async fn seed_stale_cache(store: &Store, records: &[Record]) -> Result<()> {
    let write_seq = store.read_write_seq();
    let mut stale: Vec<Record> = records
        .iter()
        .filter(|r| {
            matches!(
                r.staleness.tier,
                StalenessTier::Stale | StalenessTier::Liability | StalenessTier::Tombstone
            )
        })
        .cloned()
        .collect();
    stale.sort_by(|a, b| {
        effective_staleness(b)
            .partial_cmp(&effective_staleness(a))
            .unwrap_or(std::cmp::Ordering::Equal)
    });
    let entry = StaleCache {
        write_seq,
        records: stale,
    };
    let mut rec = cache_record(String::new());
    rec.payload = serde_json::to_value(&entry).ok();
    store.put(STALE_CACHE_KEY, &rec).await?;
    Ok(())
}

// ── Args ─────────────────────────────────────────────────────────────────────

// ── doctor freshness helper ───────────────────────────────────────────────────

/// Stale-record counts by tier — used by `mati doctor`'s freshness check.
pub(crate) struct StaleSummary {
    pub total: usize,
    pub stale: usize,
    pub liability: usize,
    pub tombstone: usize,
}

/// Read stale counts from the write-seq-validated cache. Returns `None` when no
/// valid cache exists (the caller treats that as "unknown — run `mati stale`").
/// Cheap: a single cache read, never a full scan.
pub(crate) async fn cached_stale_summary(proxy: &StoreProxy) -> Option<StaleSummary> {
    let current_seq = proxy.read_write_seq();
    let cached = proxy.get(STALE_CACHE_KEY).await.ok()??;
    let entry = cached.payload_as::<StaleCache>()?;
    if entry.write_seq != current_seq {
        return None;
    }
    let mut s = StaleSummary {
        total: entry.records.len(),
        stale: 0,
        liability: 0,
        tombstone: 0,
    };
    for r in &entry.records {
        match r.staleness.tier {
            StalenessTier::Stale => s.stale += 1,
            StalenessTier::Liability => s.liability += 1,
            StalenessTier::Tombstone => s.tombstone += 1,
            _ => {}
        }
    }
    Some(s)
}

// ── Args ─────────────────────────────────────────────────────────────────────

#[derive(Args)]
pub struct StaleArgs {
    /// Show full signal details and action hints per record
    #[arg(long, short = 'v')]
    pub verbose: bool,
    /// Maximum results to show
    #[arg(long, short = 'n', default_value = "50")]
    pub limit: usize,
}

// ── Main ─────────────────────────────────────────────────────────────────────

pub async fn run(args: StaleArgs) -> Result<()> {
    let cwd = std::env::current_dir()?;
    let proxy = StoreProxy::open(&cwd).await?;

    // ── Cache check: reuse when write-seq unchanged ───────────────────────────
    let current_seq = proxy.read_write_seq();
    if let Some(cached) = proxy.get(STALE_CACHE_KEY).await? {
        if let Some(entry) = cached.payload_as::<StaleCache>() {
            if entry.write_seq == current_seq {
                let mut stale = entry.records;
                stale.truncate(args.limit);
                proxy.close().await?;
                display_stale(&stale, args.verbose);
                return Ok(());
            }
        }
    }

    // ── Cache miss: scan all four prefixes concurrently ───────────────────────
    let (gotchas, decisions, files, notes) = tokio::try_join!(
        proxy.scan_prefix("gotcha:"),
        proxy.scan_prefix("decision:"),
        proxy.scan_prefix("file:"),
        proxy.scan_prefix("dev_note:"),
    )?;

    let mut stale: Vec<Record> = gotchas
        .into_iter()
        .chain(decisions)
        .chain(files)
        .chain(notes)
        .filter(|r| {
            matches!(
                r.staleness.tier,
                StalenessTier::Stale | StalenessTier::Liability | StalenessTier::Tombstone
            )
        })
        .collect();

    // Sort by staleness descending
    stale.sort_by(|a, b| {
        effective_staleness(b)
            .partial_cmp(&effective_staleness(a))
            .unwrap_or(std::cmp::Ordering::Equal)
    });

    // Write cache (full sorted list, before limit)
    let cache_entry = StaleCache {
        write_seq: current_seq,
        records: stale.clone(),
    };
    let mut rec = cache_record(String::new());
    rec.payload = serde_json::to_value(&cache_entry).ok();
    let _ = proxy.put(STALE_CACHE_KEY, &rec).await;

    // Limit
    stale.truncate(args.limit);

    proxy.close().await?;
    display_stale(&stale, args.verbose);
    Ok(())
}

fn display_stale(stale: &[Record], verbose: bool) {
    if stale.is_empty() {
        println!("No stale records.");
        return;
    }

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

    // ── Compact table ────────────────────────────────────────────────────────

    let mut table = Table::new();
    table
        .load_preset(UTF8_FULL_CONDENSED)
        .set_content_arrangement(ContentArrangement::Dynamic)
        .set_header(vec![
            Cell::new("Key"),
            Cell::new("Score"),
            Cell::new("Tier"),
            Cell::new("Age"),
            Cell::new("Signals"),
            Cell::new("Impact"),
        ]);

    if !use_color {
        table.force_no_tty();
    }

    let now = now_secs();

    for r in stale {
        let age_days = if r.updated_at > 0 {
            (now.saturating_sub(r.updated_at)) / 86400
        } else {
            0
        };

        let tier_color = staleness_comfy_color(&r.staleness.tier);

        // Check for propagated staleness [P] marker
        let has_propagation = r
            .payload_as::<mati_core::store::FileRecord>()
            .and_then(|fr| fr.propagated_staleness)
            .is_some_and(|p| p.value > 0.0);
        let prop_marker = if has_propagation { " [P]" } else { "" };

        table.add_row(vec![
            Cell::new(format!("{}{prop_marker}", truncate(&r.key, 40))).fg(Color::White),
            Cell::new(format!("{:.2}", r.staleness.value)).fg(tier_color),
            Cell::new(tier_short_label(&r.staleness.tier)).fg(tier_color),
            Cell::new(format!("{age_days}d")).fg(Color::Grey),
            Cell::new(summarize_signals(&r.staleness.signals, 2)).fg(Color::Grey),
            Cell::new(impact_label(&r.staleness.tier)).fg(tier_color),
        ]);
    }

    println!("{table}");

    // ── Summary + action hints ───────────────────────────────────────────────

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

    let n_liability = stale
        .iter()
        .filter(|r| r.staleness.tier == StalenessTier::Liability)
        .count();
    let n_tombstone = stale
        .iter()
        .filter(|r| r.staleness.tier == StalenessTier::Tombstone)
        .count();
    let n_stale = stale
        .iter()
        .filter(|r| r.staleness.tier == StalenessTier::Stale)
        .count();

    let mut parts: Vec<String> = Vec::new();
    if n_tombstone > 0 {
        parts.push(format!("{red}{n_tombstone} tombstone{reset}"));
    }
    if n_liability > 0 {
        parts.push(format!("{red}{n_liability} liability{reset}"));
    }
    if n_stale > 0 {
        parts.push(format!("{yellow}{n_stale} stale{reset}"));
    }
    let breakdown = if parts.is_empty() {
        String::new()
    } else {
        format!(" ({})", parts.join(", "))
    };

    println!(
        "\n  {bold}{} stale records{reset}{breakdown}\n",
        stale.len()
    );

    let mut actions: Vec<String> = Vec::new();
    for r in stale {
        let hint = action_hint(r);
        if !actions.contains(&hint) {
            actions.push(hint);
        }
        if actions.len() >= 5 {
            break;
        }
    }

    if !actions.is_empty() {
        println!("  {gray}Suggested actions:{reset}");
        for a in &actions {
            println!("    {a}");
        }
        println!();
    }

    // ── Verbose per-record blocks ────────────────────────────────────────────

    if verbose {
        println!();
        for r in stale {
            let age_days = if r.updated_at > 0 {
                (now.saturating_sub(r.updated_at)) / 86400
            } else {
                0
            };
            let stc = if use_color {
                staleness_color(&r.staleness.tier)
            } else {
                ""
            };
            let tier_label = tier_short_label(&r.staleness.tier).to_uppercase();

            println!(
                "  {stc}{bold}\u{25cf} {tier_label:<11}{reset} {bold}{key}{reset}  {gray}age: {age_days}d{reset}",
                key = r.key
            );
            println!(
                "               {gray}Score: {:.2}  Updated: {}{reset}",
                r.staleness.value,
                format_date(r.updated_at),
            );

            if !r.staleness.signals.is_empty() {
                let full_sigs = summarize_signals(&r.staleness.signals, r.staleness.signals.len());
                println!("               {gray}Signals: {full_sigs}{reset}");
            }

            println!("               \u{2192} Action: {}\n", action_hint(r));
        }
    }
}

// ── Helpers ──────────────────────────────────────────────────────────────────

/// Summarize staleness signals into a compact string.
/// Takes the first `max` signals and joins them with ", ".
/// Appends " +N more" if there are more signals than `max`.
fn summarize_signals(signals: &[StalenessSignal], max: usize) -> String {
    if signals.is_empty() {
        return String::new();
    }

    let labels: Vec<String> = signals.iter().take(max).map(signal_short_label).collect();
    let mut result = labels.join(", ");

    if signals.len() > max {
        result.push_str(&format!(" +{} more", signals.len() - max));
    }

    result
}

/// Map a single StalenessSignal to a short human-readable label.
fn signal_short_label(signal: &StalenessSignal) -> String {
    match signal {
        StalenessSignal::NotAccessedDays(n) => format!("unused {n}d"),
        StalenessSignal::EntryPointsChanged(n) => format!("{n} EP changed"),
        StalenessSignal::ImportsChanged(n) => format!("imports \u{00b1}{n}"),
        StalenessSignal::FileDeleted => "file deleted".to_string(),
        StalenessSignal::FileRenamed { .. } => "renamed".to_string(),
        StalenessSignal::LinkedFileChanged { .. } => "linked file changed".to_string(),
        StalenessSignal::TodosChanged => "TODOs changed".to_string(),
        StalenessSignal::UnsafeCountChanged(n) => format!("unsafe \u{00b1}{}", n.abs()),
        StalenessSignal::UnwrapCountChanged(n) => format!("unwrap \u{00b1}{}", n.abs()),
        StalenessSignal::DependencyBumped { dep, .. } => format!("dep:{dep} bumped"),
        StalenessSignal::LinesChangedPct(p) => format!("{:.0}% changed", p * 100.0),
        StalenessSignal::CascadeFromDecision(_) => "cascade".to_string(),
        StalenessSignal::GitCommitsSince(n) => format!("{n} commits"),
    }
}

/// Map a StalenessTier to a short impact label for the table.
fn impact_label(tier: &StalenessTier) -> &'static str {
    match tier {
        StalenessTier::Stale => "warn in bootstrap",
        StalenessTier::Liability => "blocks injection",
        StalenessTier::Tombstone => "excluded entirely",
        StalenessTier::Fresh | StalenessTier::Aging => "",
    }
}

/// Short tier label for the compact table (no extra description).
fn tier_short_label(tier: &StalenessTier) -> &'static str {
    match tier {
        StalenessTier::Fresh => "Fresh",
        StalenessTier::Aging => "Aging",
        StalenessTier::Stale => "Stale",
        StalenessTier::Liability => "Liability",
        StalenessTier::Tombstone => "Tombstone",
    }
}

/// Generate an action hint based on record category and staleness tier.
fn action_hint(record: &Record) -> String {
    match record.category {
        Category::Gotcha => format!("mati show {}", record.key),
        Category::File => {
            if record.staleness.tier == StalenessTier::Tombstone {
                let path = record.key.strip_prefix("file:").unwrap_or(&record.key);
                format!("file may be deleted \u{2014} verify: {path}")
            } else {
                let path = record.key.strip_prefix("file:").unwrap_or(&record.key);
                format!("mati reparse {path}")
            }
        }
        Category::Decision | Category::DevNote => format!("mati show {}", record.key),
        _ => format!("mati show {}", record.key),
    }
}

/// Map StalenessTier to comfy_table Color for table cells.
fn staleness_comfy_color(tier: &StalenessTier) -> Color {
    match tier {
        StalenessTier::Fresh | StalenessTier::Aging => Color::Green,
        StalenessTier::Stale => Color::Yellow,
        StalenessTier::Liability | StalenessTier::Tombstone => Color::Red,
    }
}

// ── Tests ────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use mati_core::store::{
        ConfidenceScore, QualityScore, RecordLifecycle, RecordSource, RecordVersion, StalenessScore,
    };

    fn make_record(key: &str, category: Category, tier: StalenessTier) -> Record {
        Record {
            key: key.to_string(),
            value: String::new(),
            category,
            priority: mati_core::store::Priority::Normal,
            tags: vec![],
            created_at: 0,
            updated_at: 0,
            ref_url: None,
            staleness: StalenessScore {
                value: 0.5,
                tier,
                signals: vec![],
                computed_at: 0,
                last_record_sha: String::new(),
            },
            lifecycle: RecordLifecycle::Active,
            version: RecordVersion {
                device_id: uuid::Uuid::nil(),
                logical_clock: 1,
                wall_clock: 0,
            },
            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,
            payload: None,
        }
    }

    // ── summarize_signals ────────────────────────────────────────────────────

    #[test]
    fn summarize_signals_empty() {
        assert_eq!(summarize_signals(&[], 2), "");
    }

    #[test]
    fn summarize_signals_one_signal() {
        let signals = vec![StalenessSignal::FileDeleted];
        assert_eq!(summarize_signals(&signals, 2), "file deleted");
    }

    #[test]
    fn summarize_signals_truncated() {
        let signals = vec![
            StalenessSignal::EntryPointsChanged(2),
            StalenessSignal::ImportsChanged(1),
            StalenessSignal::FileDeleted,
            StalenessSignal::TodosChanged,
        ];
        let result = summarize_signals(&signals, 2);
        assert!(result.contains("2 EP changed"));
        assert!(result.contains("imports"));
        assert!(result.contains("+2 more"));
    }

    // ── impact_label ─────────────────────────────────────────────────────────

    #[test]
    fn impact_label_for_each_tier() {
        assert_eq!(impact_label(&StalenessTier::Fresh), "");
        assert_eq!(impact_label(&StalenessTier::Aging), "");
        assert_eq!(impact_label(&StalenessTier::Stale), "warn in bootstrap");
        assert_eq!(impact_label(&StalenessTier::Liability), "blocks injection");
        assert_eq!(impact_label(&StalenessTier::Tombstone), "excluded entirely");
    }

    // ── action_hint ──────────────────────────────────────────────────────────

    #[test]
    fn action_hint_for_gotcha() {
        let r = make_record(
            "gotcha:inference-async",
            Category::Gotcha,
            StalenessTier::Stale,
        );
        assert_eq!(action_hint(&r), "mati show gotcha:inference-async");
    }

    #[test]
    fn action_hint_for_file() {
        let r = make_record("file:src/main.rs", Category::File, StalenessTier::Stale);
        assert_eq!(action_hint(&r), "mati reparse src/main.rs");
    }

    #[test]
    fn action_hint_for_file_tombstone() {
        let r = make_record("file:src/old.rs", Category::File, StalenessTier::Tombstone);
        let hint = action_hint(&r);
        assert!(hint.contains("file may be deleted"));
        assert!(hint.contains("src/old.rs"));
    }

    #[test]
    fn action_hint_for_decision() {
        let r = make_record(
            "decision:storage-engine",
            Category::Decision,
            StalenessTier::Liability,
        );
        assert_eq!(action_hint(&r), "mati show decision:storage-engine");
    }

    // ── M-15 Category 7 gap coverage ────────────────────────────────────────

    /// 7.02: Only Stale, Liability, Tombstone tiers appear in the stale list;
    /// Fresh and Aging are filtered out.
    #[test]
    fn stale_command_filters_only_stale_tiers() {
        // Simulate the filter logic from run(): only Stale|Liability|Tombstone pass.
        let records = [
            make_record("file:fresh.rs", Category::File, StalenessTier::Fresh),
            make_record("file:aging.rs", Category::File, StalenessTier::Aging),
            make_record("file:stale.rs", Category::File, StalenessTier::Stale),
            make_record(
                "file:liability.rs",
                Category::File,
                StalenessTier::Liability,
            ),
            make_record(
                "file:tombstone.rs",
                Category::File,
                StalenessTier::Tombstone,
            ),
        ];

        let stale: Vec<&Record> = records
            .iter()
            .filter(|r| {
                matches!(
                    r.staleness.tier,
                    StalenessTier::Stale | StalenessTier::Liability | StalenessTier::Tombstone
                )
            })
            .collect();

        assert_eq!(
            stale.len(),
            3,
            "only Stale, Liability, Tombstone should pass filter"
        );
        assert!(stale.iter().all(|r| !matches!(
            r.staleness.tier,
            StalenessTier::Fresh | StalenessTier::Aging
        )));
    }

    /// 7.03: Records are sorted by staleness descending.
    #[test]
    fn stale_command_sorts_descending() {
        let mut r1 = make_record("file:a.rs", Category::File, StalenessTier::Stale);
        r1.staleness.value = 0.50;
        let mut r2 = make_record("file:b.rs", Category::File, StalenessTier::Liability);
        r2.staleness.value = 0.70;
        let mut r3 = make_record("file:c.rs", Category::File, StalenessTier::Tombstone);
        r3.staleness.value = 0.95;

        let mut stale = [r1, r2, r3];
        stale.sort_by(|a, b| {
            b.staleness
                .value
                .partial_cmp(&a.staleness.value)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        assert_eq!(
            stale[0].key, "file:c.rs",
            "highest staleness should be first"
        );
        assert_eq!(stale[1].key, "file:b.rs", "second highest should be second");
        assert_eq!(stale[2].key, "file:a.rs", "lowest staleness should be last");
    }

    /// 7.04: truncate respects limit — seed 10, limit 3 → only 3 shown.
    #[test]
    fn stale_command_respects_limit() {
        let mut stale: Vec<Record> = (0..10)
            .map(|i| {
                let mut r = make_record(
                    &format!("file:mod_{i}.rs"),
                    Category::File,
                    StalenessTier::Stale,
                );
                r.staleness.value = 0.5 + (i as f32) * 0.04;
                r
            })
            .collect();

        let limit = 3;
        stale.truncate(limit);

        assert_eq!(stale.len(), limit, "truncate should cap results at {limit}");
    }

    /// 7.05: Every StalenessSignal variant produces a non-empty short label.
    #[test]
    fn signal_short_label_all_variants() {
        let variants: Vec<StalenessSignal> = vec![
            StalenessSignal::NotAccessedDays(30),
            StalenessSignal::EntryPointsChanged(2),
            StalenessSignal::ImportsChanged(5),
            StalenessSignal::FileDeleted,
            StalenessSignal::FileRenamed {
                new_path: "src/new.rs".to_string(),
            },
            StalenessSignal::LinkedFileChanged {
                path: "src/bar.rs".to_string(),
            },
            StalenessSignal::TodosChanged,
            StalenessSignal::UnsafeCountChanged(3),
            StalenessSignal::UnwrapCountChanged(-2),
            StalenessSignal::DependencyBumped {
                dep: "tokio".to_string(),
                old_ver: "1.0".to_string(),
                new_ver: "2.0".to_string(),
            },
            StalenessSignal::LinesChangedPct(0.75),
            StalenessSignal::CascadeFromDecision("decision:arch".to_string()),
        ];

        for variant in &variants {
            let label = signal_short_label(variant);
            assert!(
                !label.is_empty(),
                "signal_short_label for {:?} should produce non-empty string",
                variant
            );
        }
    }

    /// 7.06: impact_label returns a non-empty string for actionable tiers
    /// and empty string for non-actionable tiers.
    #[test]
    fn impact_label_all_tiers() {
        let all_tiers = [
            StalenessTier::Fresh,
            StalenessTier::Aging,
            StalenessTier::Stale,
            StalenessTier::Liability,
            StalenessTier::Tombstone,
        ];
        for tier in &all_tiers {
            let label = impact_label(tier);
            match tier {
                StalenessTier::Fresh | StalenessTier::Aging => {
                    assert_eq!(label, "", "Fresh/Aging should have empty impact label");
                }
                _ => {
                    assert!(
                        !label.is_empty(),
                        "{:?} should have a non-empty impact label",
                        tier
                    );
                }
            }
        }
    }

    /// 7.07: action_hint produces a meaningful hint for every category + tier combination.
    #[test]
    fn action_hint_for_each_category() {
        let categories_and_keys = vec![
            (Category::Gotcha, "gotcha:test"),
            (Category::File, "file:src/main.rs"),
            (Category::Decision, "decision:arch"),
            (Category::DevNote, "dev_note:tip"),
            (Category::Dependency, "dep:cargo:tokio"),
        ];

        for (cat, key) in &categories_and_keys {
            // Test with Stale tier
            let r = make_record(key, cat.clone(), StalenessTier::Stale);
            let hint = action_hint(&r);
            assert!(
                !hint.is_empty(),
                "action_hint for category {:?} should produce non-empty hint",
                cat
            );
        }

        // File + Tombstone specifically should mention "deleted"
        let r = make_record("file:src/gone.rs", Category::File, StalenessTier::Tombstone);
        let hint = action_hint(&r);
        assert!(
            hint.contains("deleted"),
            "File Tombstone action hint should mention deletion, got: {hint}"
        );
    }

    #[test]
    fn sort_by_effective_staleness_propagated_first() {
        use mati_core::analysis::propagation::PropagatedStaleness;

        // File A: local 0.3, propagated 0.55 → effective 0.55
        let fr_a = FileRecord {
            path: "src/a.rs".into(),
            purpose: String::new(),
            entry_points: vec![],
            imports: vec![],
            gotcha_keys: vec![],
            decision_keys: vec![],
            todos: vec![],
            unsafe_count: 0,
            unwrap_count: 0,
            change_frequency: 0,
            last_author: None,
            is_hotspot: false,
            token_cost_estimate: 0,
            last_modified_session: 0,
            content_hash: None,
            line_count: 0,
            blast_radius: None,
            propagated_staleness: Some(PropagatedStaleness {
                value: 0.55,
                source_count: 1,
                primary_source: Some("src/upstream.rs".into()),
            }),
        };
        let rec_a = Record {
            key: "file:src/a.rs".into(),
            value: String::new(),
            category: Category::File,
            priority: Priority::Normal,
            tags: vec![],
            created_at: 0,
            updated_at: 0,
            ref_url: None,
            staleness: StalenessScore {
                value: 0.3,
                tier: StalenessTier::Aging,
                signals: vec![],
                computed_at: 0,
                last_record_sha: String::new(),
            },
            lifecycle: RecordLifecycle::Active,
            version: RecordVersion {
                device_id: uuid::Uuid::new_v4(),
                logical_clock: 1,
                wall_clock: 0,
            },
            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,
            payload: serde_json::to_value(&fr_a).ok(),
        };

        // File B: local 0.45, no propagation → effective 0.45
        let fr_b = FileRecord {
            path: "src/b.rs".into(),
            purpose: String::new(),
            entry_points: vec![],
            imports: vec![],
            gotcha_keys: vec![],
            decision_keys: vec![],
            todos: vec![],
            unsafe_count: 0,
            unwrap_count: 0,
            change_frequency: 0,
            last_author: None,
            is_hotspot: false,
            token_cost_estimate: 0,
            last_modified_session: 0,
            content_hash: None,
            line_count: 0,
            blast_radius: None,
            propagated_staleness: None,
        };
        let mut rec_b = rec_a.clone();
        rec_b.key = "file:src/b.rs".into();
        rec_b.staleness.value = 0.45;
        rec_b.staleness.tier = StalenessTier::Stale;
        rec_b.payload = serde_json::to_value(&fr_b).ok();

        // Sort: A (effective 0.55) should come before B (effective 0.45)
        let mut records = [rec_b, rec_a]; // B first intentionally
        records.sort_by(|a, b| {
            effective_staleness(b)
                .partial_cmp(&effective_staleness(a))
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        assert_eq!(
            records[0].key, "file:src/a.rs",
            "A (effective 0.55) should sort before B (effective 0.45)"
        );
        assert_eq!(records[1].key, "file:src/b.rs");
    }
}