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
//! Onboarding score computation (M-10-F).
//!
//! Estimates how many minutes a new developer needs to become productive,
//! based on the knowledge coverage of the codebase.
//!
//! Formula (ARCHITECTURE.md §13.3):
//!
//! ```text
//! base_time = 22 minutes
//!
//! weighted_reduction =
//!     hotspot_coverage  * 0.40
//!   + gotcha_coverage   * 0.25
//!   + decision_coverage * 0.15
//!   + avg_confidence    * 0.20
//!
//! estimated_minutes = base_time * (1 - weighted_reduction)
//! ```
//!
//! Each factor is a fraction in `[0.0, 1.0]`:
//! - `hotspot_coverage`:  fraction of hotspot files with non-empty `purpose`
//! - `gotcha_coverage`:   fraction of hotspot files with at least one gotcha key
//! - `decision_coverage`: fraction of `decision:*` records with non-empty value
//! - `avg_confidence`:    mean confidence across records with `confidence >= 0.6`

use std::time::{SystemTime, UNIX_EPOCH};

use anyhow::Result;

use crate::store::{FileRecord, OnboardingScore, Store};

// ── Constants ────────────────────────────────────────────────────────────────

/// Baseline onboarding time (minutes) for a completely undocumented codebase.
const BASE_TIME: f32 = 22.0;

/// Weight for hotspot file purpose coverage.
const W_HOTSPOT: f32 = 0.40;
/// Weight for gotcha coverage on hotspot files.
const W_GOTCHA: f32 = 0.25;
/// Weight for architectural decision documentation.
const W_DECISION: f32 = 0.15;
/// Weight for average confidence across confirmed records.
const W_CONFIDENCE: f32 = 0.20;

/// Minimum confidence value for a record to count as "confirmed".
const CONFIDENCE_THRESHOLD: f32 = 0.6;

// ── Public API ───────────────────────────────────────────────────────────────

/// Compute the [`OnboardingScore`] from pre-loaded record slices.
///
/// Use this when the caller already has the store data to avoid redundant scans.
pub fn compute_from_records(
    file_records: &[crate::store::Record],
    decisions: &[crate::store::Record],
    gotchas: &[crate::store::Record],
) -> OnboardingScore {
    let file_data: Vec<FileRecord> = file_records
        .iter()
        .filter_map(|r| r.payload_as::<FileRecord>())
        .collect();
    let hotspot_coverage = compute_hotspot_coverage(&file_data);
    let gotcha_coverage = compute_gotcha_coverage(&file_data);
    let decision_coverage = compute_decision_coverage(decisions);
    let all_knowledge: Vec<_> = gotchas.iter().chain(decisions.iter()).collect();
    let avg_confidence = compute_avg_confidence(&all_knowledge);
    let estimated_minutes = compute_estimated_minutes(
        hotspot_coverage,
        gotcha_coverage,
        decision_coverage,
        avg_confidence,
    );
    let now = SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .unwrap_or_default()
        .as_secs();
    OnboardingScore {
        estimated_minutes,
        critical_files_covered: hotspot_coverage,
        gotcha_coverage,
        decision_coverage,
        avg_confidence,
        computed_at: now,
    }
}

/// Compute the [`OnboardingScore`] by scanning the store for file, decision,
/// and gotcha records.
pub async fn compute(store: &Store) -> Result<OnboardingScore> {
    let file_records = store.scan_prefix("file:").await?;
    let decision_records = store.scan_prefix("decision:").await?;
    let gotcha_records = store.scan_prefix("gotcha:").await?;
    Ok(compute_from_records(
        &file_records,
        &decision_records,
        &gotcha_records,
    ))
}

// ── Pure helpers (testable without Store) ────────────────────────────────────

/// Fraction of hotspot files with a non-empty `purpose` field.
/// Returns 0.0 if there are no hotspot files.
fn compute_hotspot_coverage(files: &[FileRecord]) -> f32 {
    let hotspots: Vec<&FileRecord> = files.iter().filter(|f| f.is_hotspot).collect();
    if hotspots.is_empty() {
        return 0.0;
    }
    let covered = hotspots.iter().filter(|f| !f.purpose.is_empty()).count();
    covered as f32 / hotspots.len() as f32
}

/// Fraction of hotspot files with at least one gotcha key.
/// Returns 0.0 if there are no hotspot files.
fn compute_gotcha_coverage(files: &[FileRecord]) -> f32 {
    let hotspots: Vec<&FileRecord> = files.iter().filter(|f| f.is_hotspot).collect();
    if hotspots.is_empty() {
        return 0.0;
    }
    let covered = hotspots
        .iter()
        .filter(|f| !f.gotcha_keys.is_empty())
        .count();
    covered as f32 / hotspots.len() as f32
}

/// Fraction of `decision:*` records with a non-empty value (enriched vs stub).
/// Returns 0.0 if no decisions exist.
fn compute_decision_coverage(decisions: &[crate::store::Record]) -> f32 {
    if decisions.is_empty() {
        return 0.0;
    }
    let enriched = decisions
        .iter()
        .filter(|r| !r.value.trim().is_empty())
        .count();
    enriched as f32 / decisions.len() as f32
}

/// Average `confidence.value` across records where `confidence.value >= 0.6`.
/// Returns 0.0 if no records meet the threshold.
fn compute_avg_confidence(records: &[&crate::store::Record]) -> f32 {
    let qualifying: Vec<f32> = records
        .iter()
        .filter(|r| r.confidence.value >= CONFIDENCE_THRESHOLD)
        .map(|r| r.confidence.value)
        .collect();
    if qualifying.is_empty() {
        return 0.0;
    }
    qualifying.iter().sum::<f32>() / qualifying.len() as f32
}

/// Apply the onboarding formula to produce estimated minutes.
fn compute_estimated_minutes(
    hotspot_coverage: f32,
    gotcha_coverage: f32,
    decision_coverage: f32,
    avg_confidence: f32,
) -> f32 {
    let weighted_reduction = hotspot_coverage * W_HOTSPOT
        + gotcha_coverage * W_GOTCHA
        + decision_coverage * W_DECISION
        + avg_confidence * W_CONFIDENCE;

    // Clamp reduction to [0, 1] to avoid negative minutes.
    let clamped = weighted_reduction.clamp(0.0, 1.0);
    BASE_TIME * (1.0 - clamped)
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::store::{
        Category, ConfidenceScore, Priority, QualityScore, Record, RecordLifecycle, RecordSource,
        RecordVersion, StalenessScore, TodoComment,
    };
    use uuid::Uuid;

    fn device_id() -> Uuid {
        Uuid::parse_str("550e8400-e29b-41d4-a716-446655440000").unwrap()
    }

    fn make_file_record(
        path: &str,
        purpose: &str,
        gotcha_keys: Vec<String>,
        is_hotspot: bool,
    ) -> FileRecord {
        FileRecord {
            path: path.into(),
            purpose: purpose.into(),
            entry_points: vec![],
            imports: vec![],
            gotcha_keys,
            decision_keys: vec![],
            todos: Vec::<TodoComment>::new(),
            unsafe_count: 0,
            unwrap_count: 0,
            change_frequency: if is_hotspot { 50 } else { 5 },
            last_author: None,
            is_hotspot,
            token_cost_estimate: 200,
            last_modified_session: 0,
            content_hash: None,
            line_count: 0,
            blast_radius: None,
            propagated_staleness: None,
        }
    }

    fn make_record(key: &str, value: &str, confidence_value: f32) -> Record {
        Record {
            key: key.into(),
            value: value.into(),
            category: if key.starts_with("gotcha:") {
                Category::Gotcha
            } else if key.starts_with("decision:") {
                Category::Decision
            } else {
                Category::File
            },
            priority: Priority::Normal,
            tags: vec![],
            created_at: 1_710_520_800,
            updated_at: 1_710_520_800,
            ref_url: None,
            staleness: StalenessScore::fresh(),
            lifecycle: RecordLifecycle::Active,
            version: RecordVersion {
                device_id: device_id(),
                logical_clock: 1,
                wall_clock: 1_710_520_800,
            },
            quality: QualityScore::layer0_default(),
            access_count: 0,
            last_accessed: 0,
            source: RecordSource::DeveloperManual,
            confidence: ConfidenceScore {
                value: confidence_value,
                confirmation_count: if confidence_value >= 0.6 { 1 } else { 0 },
                contributor_count: 1,
                last_challenged: None,
                challenge_count: 0,
            },
            gap_analysis_score: 0.0,
            payload: None,
        }
    }

    // ── Formula tests ────────────────────────────────────────────────────

    #[test]
    fn no_records_yields_base_time() {
        let minutes = compute_estimated_minutes(0.0, 0.0, 0.0, 0.0);
        assert!(
            (minutes - BASE_TIME).abs() < f32::EPSILON,
            "expected {BASE_TIME}, got {minutes}"
        );
    }

    #[test]
    fn full_coverage_yields_near_zero() {
        let minutes = compute_estimated_minutes(1.0, 1.0, 1.0, 1.0);
        assert!(minutes.abs() < 0.01, "expected ~0.0, got {minutes}");
    }

    #[test]
    fn partial_coverage_proportional_reduction() {
        // 50% hotspot coverage only: reduction = 0.5 * 0.40 = 0.20
        let minutes = compute_estimated_minutes(0.5, 0.0, 0.0, 0.0);
        let expected = BASE_TIME * (1.0 - 0.20);
        assert!(
            (minutes - expected).abs() < 0.01,
            "expected {expected}, got {minutes}"
        );
    }

    // ── Hotspot coverage ─────────────────────────────────────────────────

    #[test]
    fn hotspot_coverage_no_hotspots() {
        let files = vec![make_file_record(
            "src/lib.rs",
            "library root",
            vec![],
            false,
        )];
        assert!((compute_hotspot_coverage(&files) - 0.0).abs() < f32::EPSILON);
    }

    #[test]
    fn hotspot_coverage_all_documented() {
        let files = vec![
            make_file_record("src/main.rs", "entry point", vec![], true),
            make_file_record("src/db.rs", "database layer", vec![], true),
        ];
        assert!((compute_hotspot_coverage(&files) - 1.0).abs() < f32::EPSILON);
    }

    #[test]
    fn hotspot_coverage_partial() {
        let files = vec![
            make_file_record("src/main.rs", "entry point", vec![], true),
            make_file_record("src/db.rs", "", vec![], true), // empty purpose
        ];
        assert!((compute_hotspot_coverage(&files) - 0.5).abs() < f32::EPSILON);
    }

    // ── Gotcha coverage ──────────────────────────────────────────────────

    #[test]
    fn gotcha_coverage_no_hotspots() {
        let files = vec![make_file_record(
            "src/lib.rs",
            "",
            vec!["gotcha:x".into()],
            false,
        )];
        assert!((compute_gotcha_coverage(&files) - 0.0).abs() < f32::EPSILON);
    }

    #[test]
    fn gotcha_coverage_all_have_gotchas() {
        let files = vec![
            make_file_record("src/main.rs", "", vec!["gotcha:a".into()], true),
            make_file_record("src/db.rs", "", vec!["gotcha:b".into()], true),
        ];
        assert!((compute_gotcha_coverage(&files) - 1.0).abs() < f32::EPSILON);
    }

    #[test]
    fn gotcha_coverage_partial() {
        let files = vec![
            make_file_record("src/main.rs", "", vec!["gotcha:a".into()], true),
            make_file_record("src/db.rs", "", vec![], true), // no gotchas
        ];
        assert!((compute_gotcha_coverage(&files) - 0.5).abs() < f32::EPSILON);
    }

    // ── Decision coverage ────────────────────────────────────────────────

    #[test]
    fn decision_coverage_no_decisions() {
        let records: Vec<Record> = vec![];
        assert!((compute_decision_coverage(&records) - 0.0).abs() < f32::EPSILON);
    }

    #[test]
    fn decision_coverage_all_enriched() {
        let records = vec![
            make_record(
                "decision:use-surrealkv",
                "We chose SurrealKV because...",
                0.8,
            ),
            make_record(
                "decision:three-tools",
                "MCP tools capped at 3 to save tokens",
                0.7,
            ),
        ];
        assert!((compute_decision_coverage(&records) - 1.0).abs() < f32::EPSILON);
    }

    #[test]
    fn decision_coverage_half_stubs() {
        let records = vec![
            make_record(
                "decision:use-surrealkv",
                "We chose SurrealKV because...",
                0.8,
            ),
            make_record("decision:three-tools", "", 0.1), // stub
        ];
        assert!((compute_decision_coverage(&records) - 0.5).abs() < f32::EPSILON);
    }

    // ── Average confidence ───────────────────────────────────────────────

    #[test]
    fn avg_confidence_no_qualifying() {
        let records = [
            make_record("gotcha:low", "some text", 0.3),
            make_record("gotcha:also-low", "other text", 0.5),
        ];
        let refs: Vec<&Record> = records.iter().collect();
        assert!((compute_avg_confidence(&refs) - 0.0).abs() < f32::EPSILON);
    }

    #[test]
    fn avg_confidence_all_qualifying() {
        let records = [
            make_record("gotcha:high", "some text", 0.8),
            make_record("decision:important", "other text", 0.6),
        ];
        let refs: Vec<&Record> = records.iter().collect();
        let expected = (0.8 + 0.6) / 2.0;
        assert!(
            (compute_avg_confidence(&refs) - expected).abs() < 0.001,
            "expected {expected}, got {}",
            compute_avg_confidence(&refs)
        );
    }

    #[test]
    fn avg_confidence_mixed() {
        let records = [
            make_record("gotcha:high", "some text", 0.9),
            make_record("gotcha:low", "other text", 0.3), // below threshold
            make_record("decision:mid", "text", 0.7),
        ];
        let refs: Vec<&Record> = records.iter().collect();
        let expected = (0.9 + 0.7) / 2.0;
        assert!(
            (compute_avg_confidence(&refs) - expected).abs() < 0.001,
            "expected {expected}, got {}",
            compute_avg_confidence(&refs)
        );
    }

    // ── Individual factor weights ────────────────────────────────────────

    #[test]
    fn hotspot_factor_only() {
        // Full hotspot coverage, nothing else.
        let minutes = compute_estimated_minutes(1.0, 0.0, 0.0, 0.0);
        let expected = BASE_TIME * (1.0 - W_HOTSPOT);
        assert!(
            (minutes - expected).abs() < 0.01,
            "expected {expected}, got {minutes}"
        );
    }

    #[test]
    fn gotcha_factor_only() {
        let minutes = compute_estimated_minutes(0.0, 1.0, 0.0, 0.0);
        let expected = BASE_TIME * (1.0 - W_GOTCHA);
        assert!(
            (minutes - expected).abs() < 0.01,
            "expected {expected}, got {minutes}"
        );
    }

    #[test]
    fn decision_factor_only() {
        let minutes = compute_estimated_minutes(0.0, 0.0, 1.0, 0.0);
        let expected = BASE_TIME * (1.0 - W_DECISION);
        assert!(
            (minutes - expected).abs() < 0.01,
            "expected {expected}, got {minutes}"
        );
    }

    #[test]
    fn confidence_factor_only() {
        let minutes = compute_estimated_minutes(0.0, 0.0, 0.0, 1.0);
        let expected = BASE_TIME * (1.0 - W_CONFIDENCE);
        assert!(
            (minutes - expected).abs() < 0.01,
            "expected {expected}, got {minutes}"
        );
    }
}