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
//! Real-repo performance and accuracy benchmark for mati.
//!
//! Two-pass design:
//!   Pass 1 (cold) — wipe store, fresh `mati init`, run every command.
//!   Pass 2 (warm) — same commands against existing store (cache warm, OS page cache warm).
//!
//! Measures: wall-clock latency (mean/min/max/stddev), cache speedup, parse
//! accuracy, knowledge health, data integrity, and store size.
//!
//! Usage:
//!   cargo build --release
//!   cargo run --bin bench_real --release -- --repos ripgrep,deno --samples 5
//!   cargo run --bin bench_real --release -- --repos ripgrep --output BENCHMARKS.md

mod parser;
mod report;
mod repos;
mod runner;

use std::collections::HashMap;
use std::path::{Path, PathBuf};

use clap::Parser as ClapParser;
use parser::*;
use repos::{
    clean_store, compute_slug, ensure_cloned, find_spec, git_file_count, git_lang_counts, store_dir,
};
use runner::{mati_bin, run_edit_hook, run_once, run_parallel_gets, run_timed, TimedResult};

// ── CLI ───────────────────────────────────────────────────────────────────────

#[derive(ClapParser, Debug)]
#[command(
    name = "bench_real",
    about = "Real-repo mati performance + accuracy benchmark",
    long_about = "Clones repos, runs mati init (cold) then all commands twice \
                  (cold + warm), measures speed + accuracy, outputs markdown."
)]
struct Args {
    /// Repos to test: ripgrep,deno,nextjs,tokio,mati (comma-separated).
    /// Use "mati" to test against this project itself.
    #[arg(long, default_value = "ripgrep,deno,nextjs")]
    repos: String,

    /// Timing samples per command (more = more accurate, slower).
    #[arg(long, default_value_t = 5)]
    samples: usize,

    /// Directory for cloned repos (created if absent).
    #[arg(long, default_value = "/tmp/mati-bench-repos")]
    repo_cache: PathBuf,

    /// Write markdown output to this file (default: stdout).
    #[arg(long)]
    output: Option<PathBuf>,

    /// Skip the cold pass (only measure warm).
    #[arg(long)]
    warm_only: bool,

    /// Skip the warm pass (only measure cold).
    #[arg(long)]
    cold_only: bool,

    /// Max number of file keys to use for parallel-get tests.
    #[arg(long, default_value_t = 25)]
    parallel_keys: usize,
}

// ── Data model ────────────────────────────────────────────────────────────────

pub struct RepoReport {
    pub repo_name: String,
    pub cold: PassResults,
    pub warm: PassResults,
    pub accuracy: AccuracyReport,
    pub lang_counts: Vec<(String, usize)>,
}

pub struct PassResults {
    pub init: InitMetrics,
    pub commands: HashMap<String, TimedResult>,
    pub samples: usize,
}

impl PassResults {
    fn empty(samples: usize) -> Self {
        PassResults {
            init: InitMetrics::default(),
            commands: HashMap::new(),
            samples,
        }
    }
}

pub struct AccuracyReport {
    // Coverage
    pub file_count_mati: usize,
    pub file_count_git: usize,
    pub total_records: usize,
    pub confidence_avg: f64,

    // Correctness
    pub get_hit_rate_pct: u32,
    pub stats_cold_warm_consistent: bool,
    pub edit_hook_success: bool,
    pub edit_hook_staleness_changed: bool,
    pub export_success: bool,
    pub init_success: bool,
    pub harvest_success: bool,

    // Health
    pub gaps: GapsMetrics,
    pub stale: StaleMetrics,
    pub ping_us: Option<u64>,

    // Store
    pub store_size_mb: Option<f64>,
}

// ── Entry point ───────────────────────────────────────────────────────────────

fn main() {
    let args = Args::parse();

    // Locate mati binary.
    let bin = mati_bin();
    eprintln!("mati binary: {}", bin.display());
    if !bin.exists() {
        eprintln!("ERROR: mati binary not found. Run `cargo build --release` first.");
        std::process::exit(1);
    }

    // Create repo cache dir.
    std::fs::create_dir_all(&args.repo_cache).expect("failed to create --repo-cache dir");

    let repo_names: Vec<&str> = args.repos.split(',').map(str::trim).collect();
    let mut reports: Vec<RepoReport> = Vec::new();

    for name in &repo_names {
        eprintln!("\n══ {} ══════════════════════════════════", name);

        let repo_path = resolve_repo_path(name, &args.repo_cache);
        let slug = compute_slug(&repo_path);
        eprintln!("  path: {}", repo_path.display());
        eprintln!("  slug: {}", slug);

        // ── Cold pass ─────────────────────────────────────────────────────
        let cold = if !args.warm_only {
            eprintln!("\n  [cold pass]");
            eprintln!("  wiping store...");
            clean_store(&slug);
            run_pass(&bin, &repo_path, args.samples, args.parallel_keys, true)
        } else {
            PassResults::empty(args.samples)
        };

        // ── Warm pass ─────────────────────────────────────────────────────
        let warm = if !args.cold_only {
            eprintln!("\n  [warm pass]");
            run_pass(&bin, &repo_path, args.samples, args.parallel_keys, false)
        } else {
            PassResults::empty(args.samples)
        };

        // ── Accuracy ──────────────────────────────────────────────────────
        eprintln!("\n  [accuracy checks]");
        let accuracy = compute_accuracy(&bin, &repo_path, &cold, &warm, &slug);

        let lang_counts = git_lang_counts(&repo_path);

        // Use the directory basename as display name for absolute paths.
        let display_name = if name.starts_with('/') || name.starts_with('.') {
            repo_path
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or(name)
                .to_string()
        } else {
            name.to_string()
        };

        reports.push(RepoReport {
            repo_name: display_name,
            cold,
            warm,
            accuracy,
            lang_counts,
        });
    }

    // ── Report ────────────────────────────────────────────────────────────
    let date = chrono::Local::now().format("%Y-%m-%d").to_string();
    let markdown = report::generate(&reports, &date);

    if let Some(out_path) = &args.output {
        std::fs::write(out_path, &markdown).expect("failed to write output file");
        eprintln!("\nReport written to {}", out_path.display());
    } else {
        println!("{}", markdown);
    }
}

// ── Repo resolution ───────────────────────────────────────────────────────────

fn resolve_repo_path(name: &str, cache_dir: &Path) -> PathBuf {
    // Absolute path — use directly.
    if name.starts_with('/') || name.starts_with('.') {
        let p = PathBuf::from(name);
        assert!(p.exists(), "repo path does not exist: {}", name);
        return p;
    }

    if name == "mati" {
        // Use this project's root (one level above target/).
        let exe = std::env::current_exe().unwrap_or_default();
        let mut dir = exe
            .parent()
            .and_then(|p| p.parent())
            .map(|p| p.to_path_buf());
        while let Some(d) = dir {
            if d.join("Cargo.toml").exists() {
                let content = std::fs::read_to_string(d.join("Cargo.toml")).unwrap_or_default();
                if content.contains("name = \"mati\"") {
                    return d;
                }
            }
            dir = d.parent().map(|p| p.to_path_buf());
        }
        return std::env::current_dir().unwrap();
    }

    let spec = find_spec(name).unwrap_or_else(|| {
        panic!(
            "unknown repo '{}'. Known: ripgrep, deno, nextjs, tokio, mati, or an absolute path",
            name
        )
    });
    ensure_cloned(spec, cache_dir)
}

// ── Pass execution ────────────────────────────────────────────────────────────

fn run_pass(
    bin: &Path,
    repo_path: &Path,
    samples: usize,
    parallel_keys: usize,
    is_cold: bool,
) -> PassResults {
    let mut commands: HashMap<String, TimedResult> = HashMap::new();

    // ── Init (cold only) ──────────────────────────────────────────────────
    let init = if is_cold {
        eprintln!("    mati init...");
        let r = run_once(
            bin,
            &["init", "--path", repo_path.to_str().unwrap(), "--no-hooks"],
            repo_path,
        );
        if !r.success {
            eprintln!(
                "    WARN init failed (exit {}): {}",
                r.exit_code,
                r.stderr.trim()
            );
        } else {
            eprintln!("    init ok ({}ms)", r.mean_ms as u64);
        }
        parse_init(&r.stdout)
    } else {
        InitMetrics::default()
    };

    // ── Discover test keys ────────────────────────────────────────────────
    let ls_out = run_once(bin, &["ls", "files"], repo_path);
    let file_keys = extract_file_keys(&ls_out.stdout);
    let get_key = file_keys
        .first()
        .cloned()
        .unwrap_or_else(|| "file:src/lib.rs".into());

    eprintln!("    {} file keys discovered", file_keys.len());

    // ── mati status ───────────────────────────────────────────────────────
    step("status", &mut commands, || {
        run_timed(bin, &["status"], repo_path, samples)
    });

    // ── mati stats: first run = cache miss, then N more = cache hit ───────
    step("stats_first", &mut commands, || {
        run_timed(bin, &["stats"], repo_path, 1)
    });
    step("stats_avg", &mut commands, || {
        run_timed(bin, &["stats"], repo_path, samples)
    });

    // ── mati gaps ─────────────────────────────────────────────────────────
    step("gaps_first", &mut commands, || {
        run_timed(bin, &["gaps"], repo_path, 1)
    });
    step("gaps_avg", &mut commands, || {
        run_timed(bin, &["gaps"], repo_path, samples)
    });

    // ── mati ls ───────────────────────────────────────────────────────────
    step("ls_files", &mut commands, || {
        run_timed(bin, &["ls", "files"], repo_path, samples)
    });
    step("ls_gotchas", &mut commands, || {
        run_timed(bin, &["ls", "gotchas"], repo_path, samples)
    });
    step("ls_decisions", &mut commands, || {
        run_timed(bin, &["ls", "decisions"], repo_path, samples)
    });

    // ── mati stale ────────────────────────────────────────────────────────
    step("stale", &mut commands, || {
        run_timed(bin, &["stale"], repo_path, samples)
    });

    // ── mati quality-check ────────────────────────────────────────────────
    step("quality_check", &mut commands, || {
        run_timed(bin, &["quality-check"], repo_path, samples)
    });

    // ── mati get ×1 ──────────────────────────────────────────────────────
    step("get_1", &mut commands, || {
        run_timed(bin, &["get", &get_key], repo_path, samples)
    });

    // ── mati show ─────────────────────────────────────────────────────────
    step("show", &mut commands, || {
        run_timed(bin, &["show", &get_key], repo_path, samples)
    });

    // ── mati get ×10 parallel ─────────────────────────────────────────────
    {
        let keys10: Vec<String> = file_keys.iter().take(10).cloned().collect();
        step("get_10", &mut commands, || {
            run_parallel_gets(bin, &keys10, repo_path)
        });
    }

    // ── mati get ×25 parallel ─────────────────────────────────────────────
    {
        let keys25: Vec<String> = file_keys.iter().take(parallel_keys).cloned().collect();
        step("get_25", &mut commands, || {
            run_parallel_gets(bin, &keys25, repo_path)
        });
    }

    // ── mati export --format json ─────────────────────────────────────────
    step("export_json", &mut commands, || {
        run_timed(bin, &["export", "--format", "json"], repo_path, samples)
    });

    // ── mati history ──────────────────────────────────────────────────────
    step("history", &mut commands, || {
        run_timed(bin, &["history", &get_key], repo_path, samples)
    });

    // ── mati ping ─────────────────────────────────────────────────────────
    step("ping", &mut commands, || {
        run_timed(bin, &["ping"], repo_path, samples)
    });

    // ── mati edit-hook ────────────────────────────────────────────────────
    {
        let rel = get_key.strip_prefix("file:").unwrap_or("src/lib.rs");
        let abs = repo_path.join(rel);
        step("edit_hook", &mut commands, || {
            run_edit_hook(bin, &abs, repo_path, samples)
        });
    }

    // ── mati session-harvest ──────────────────────────────────────────────
    step("session_harvest", &mut commands, || {
        run_timed(bin, &["session-harvest"], repo_path, samples)
    });

    // ── mati log-miss / log-hit ───────────────────────────────────────────
    step("log_miss", &mut commands, || {
        run_timed(bin, &["log-miss", &get_key], repo_path, samples)
    });
    step("log_hit", &mut commands, || {
        run_timed(bin, &["log-hit", &get_key], repo_path, samples)
    });

    PassResults {
        init,
        commands,
        samples,
    }
}

/// Run a single step, print progress, insert result.
fn step(key: &str, commands: &mut HashMap<String, TimedResult>, f: impl FnOnce() -> TimedResult) {
    eprint!("    {}...", key);
    let r = f();
    let status = if r.success { "" } else { " [FAIL]" };
    eprintln!(" {:.0}ms{}", r.mean_ms, status);
    commands.insert(key.into(), r);
}

// ── Accuracy computation ──────────────────────────────────────────────────────

fn compute_accuracy(
    bin: &Path,
    repo_path: &Path,
    cold: &PassResults,
    warm: &PassResults,
    slug: &str,
) -> AccuracyReport {
    // File counts.
    let file_count_git = git_file_count(repo_path);
    let status_out = run_once(bin, &["status"], repo_path);
    let status = parse_status(&status_out.stdout);
    let file_count_mati = status.file_count;
    let total_records = status.file_count
        + status.gotcha_count
        + status.decision_count
        + status.note_count
        + status.dep_count;

    eprintln!("    files: mati={} git={}", file_count_mati, file_count_git);

    // Stats consistency: cold first-run vs warm first-run numbers should match.
    let cold_stats = cold
        .commands
        .get("stats_first")
        .map(|r| parse_stats(&r.stdout));
    let warm_stats = warm
        .commands
        .get("stats_first")
        .map(|r| parse_stats(&r.stdout));
    let stats_consistent = match (&cold_stats, &warm_stats) {
        (Some(c), Some(w)) => {
            (c.files_with_purpose as i64 - w.files_with_purpose as i64).abs() <= 1
                && (c.gap_count as i64 - w.gap_count as i64).abs() <= 1
        }
        _ => true, // one pass skipped — can't compare
    };

    // Get hit rate: test up to 25 known file keys.
    let ls_out = run_once(bin, &["ls", "files"], repo_path);
    let keys = extract_file_keys(&ls_out.stdout);
    let test_n = keys.len().min(25);
    let test_keys = keys.iter().take(test_n).cloned().collect::<Vec<_>>();
    let hits: usize = test_keys
        .iter()
        .filter(|k| run_once(bin, &["get", k], repo_path).success)
        .count();
    let get_hit_rate_pct = (hits * 100).checked_div(test_n).unwrap_or(0) as u32;
    eprintln!(
        "    get hit rate: {}/{}  ({}%)",
        hits, test_n, get_hit_rate_pct
    );

    // Gaps.
    let gaps_out = run_once(bin, &["gaps"], repo_path);
    let gaps = parse_gaps(&gaps_out.stdout);

    // Staleness.
    let stale_out = run_once(bin, &["stale"], repo_path);
    let stale = parse_stale(&stale_out.stdout);

    // Ping latency (parse µs from output).
    let ping_out = run_once(bin, &["ping"], repo_path);
    let ping_us = parse_ping_us(&ping_out.stdout);

    // Edit-hook accuracy: did the hook succeed? Did staleness change?
    let (edit_hook_success, edit_hook_staleness_changed) =
        check_edit_hook_accuracy(bin, repo_path, &keys);

    // Export.
    let export_out = run_once(bin, &["export", "--format", "json"], repo_path);
    let export_success = export_out.success && export_out.stdout.contains('{');

    // Init & harvest success.
    let init_success = cold.init.completed || cold.commands.is_empty();
    let harvest_success = cold
        .commands
        .get("session_harvest")
        .map(|r| r.success)
        .unwrap_or(false);

    // Store size.
    let store_size_mb = dir_size_mb(&store_dir(slug));

    AccuracyReport {
        file_count_mati,
        file_count_git,
        total_records,
        confidence_avg: status.confidence_avg,
        get_hit_rate_pct,
        stats_cold_warm_consistent: stats_consistent,
        edit_hook_success,
        edit_hook_staleness_changed,
        export_success,
        init_success,
        harvest_success,
        gaps,
        stale,
        ping_us,
        store_size_mb,
    }
}

// ── Edit-hook accuracy ────────────────────────────────────────────────────────

fn check_edit_hook_accuracy(bin: &Path, repo_path: &Path, file_keys: &[String]) -> (bool, bool) {
    let key = match file_keys.first() {
        Some(k) => k,
        None => return (false, false),
    };
    let rel = key.strip_prefix("file:").unwrap_or("");
    let abs = repo_path.join(rel);

    if !abs.exists() || !abs.is_file() {
        return (false, false);
    }

    // Snapshot confidence/staleness before.
    let before_out = run_once(bin, &["show", key], repo_path);

    // Mutate + hook.
    let hook_result = run_edit_hook(bin, &abs, repo_path, 1);
    if !hook_result.success {
        return (false, false);
    }

    // Snapshot after.
    let after_out = run_once(bin, &["show", key], repo_path);

    // A staleness change shows up in the show output (different staleness line).
    let changed = after_out.stdout != before_out.stdout;
    (true, changed)
}

// ── Filesystem helpers ────────────────────────────────────────────────────────

fn dir_size_mb(path: &Path) -> Option<f64> {
    if !path.exists() {
        return None;
    }
    let bytes = du_bytes(path)?;
    Some(bytes as f64 / 1_048_576.0)
}

fn du_bytes(path: &Path) -> Option<u64> {
    // Use `du -sk` for portability; result is in 512-byte blocks on macOS.
    let out = std::process::Command::new("du")
        .args(["-sk", path.to_str()?])
        .output()
        .ok()?;
    let s = String::from_utf8_lossy(&out.stdout);
    let kb: u64 = s.split_whitespace().next()?.parse().ok()?;
    Some(kb * 1024)
}