aprender-qa-certify 0.30.0

Model certification tools and README synchronization
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
//! Model certification tools and README synchronization.
//!
//! This crate provides utilities for:
//! - Parsing model certification CSV data
//! - Generating markdown tables for README
//! - Synchronizing certification status with documentation

#![forbid(unsafe_code)]
#![cfg_attr(
    test,
    allow(
        clippy::unwrap_used,
        clippy::expect_used,
        clippy::panic,
        clippy::doc_markdown
    )
)]

use chrono::{DateTime, Utc};
use std::fmt;
use thiserror::Error;

/// Errors that can occur during certification operations.
#[derive(Error, Debug)]
pub enum CertifyError {
    /// CSV parsing error.
    #[error("CSV parse error at line {line}: {message}")]
    CsvParse {
        /// Line number where error occurred.
        line: usize,
        /// Error message.
        message: String,
    },

    /// README marker not found.
    #[error("README marker not found: {0}")]
    MarkerNotFound(String),

    /// IO error.
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),
}

/// Result type for certification operations.
pub type Result<T> = std::result::Result<T, CertifyError>;

/// Certification status for a model.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum CertificationStatus {
    /// Model passed all tests with MQS >= 850.
    Certified,
    /// Model passed with MQS >= 700 but < 850.
    Provisional,
    /// Model failed tests or MQS < 700.
    Blocked,
    /// Model not yet tested.
    #[default]
    Pending,
}

impl CertificationStatus {
    /// Parse status from string representation.
    /// Returns `None` for unrecognized values (Jidoka: fail loud on bad data).
    #[must_use]
    pub fn parse(s: &str) -> Option<Self> {
        match s.to_uppercase().as_str() {
            "CERTIFIED" => Some(Self::Certified),
            "PROVISIONAL" => Some(Self::Provisional),
            "BLOCKED" => Some(Self::Blocked),
            "PENDING" => Some(Self::Pending),
            _ => None,
        }
    }

    /// Get badge markdown for this status.
    #[must_use]
    pub const fn badge(&self) -> &'static str {
        match self {
            Self::Certified => "![certified](https://img.shields.io/badge/CERTIFIED-brightgreen)",
            Self::Provisional => "![provisional](https://img.shields.io/badge/PROVISIONAL-yellow)",
            Self::Blocked => "![blocked](https://img.shields.io/badge/BLOCKED-red)",
            Self::Pending => "![pending](https://img.shields.io/badge/PENDING-lightgray)",
        }
    }
}

impl fmt::Display for CertificationStatus {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Certified => write!(f, "CERTIFIED"),
            Self::Provisional => write!(f, "PROVISIONAL"),
            Self::Blocked => write!(f, "BLOCKED"),
            Self::Pending => write!(f, "PENDING"),
        }
    }
}

/// Size category for models.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum SizeCategory {
    /// < 1B parameters.
    Tiny,
    /// 1B - 5B parameters.
    #[default]
    Small,
    /// 5B - 10B parameters.
    Medium,
    /// 10B - 30B parameters.
    Large,
    /// > 30B parameters.
    XLarge,
}

impl SizeCategory {
    /// Parse size category from string representation.
    /// Returns `None` for unrecognized values (Jidoka: fail loud on bad data).
    #[must_use]
    pub fn parse(s: &str) -> Option<Self> {
        match s.to_lowercase().as_str() {
            "tiny" => Some(Self::Tiny),
            "small" => Some(Self::Small),
            "medium" => Some(Self::Medium),
            "large" => Some(Self::Large),
            "xlarge" => Some(Self::XLarge),
            _ => None,
        }
    }
}

/// Model certification record.
///
/// Contains certification data for a single model including gateway status
/// and throughput measurements per format.
/// The four gateway bools (g1-g4) are required for the certification protocol.
#[allow(clippy::struct_excessive_bools)]
#[derive(Debug, Clone)]
pub struct ModelCertification {
    /// `HuggingFace` model ID.
    pub model_id: String,
    /// Model family (e.g., qwen-coder, llama).
    pub family: String,
    /// Parameter count (e.g., "1.5B").
    pub parameters: String,
    /// Size category.
    pub size_category: SizeCategory,
    /// Certification status.
    pub status: CertificationStatus,
    /// Model Qualification Score (0-1000).
    pub mqs_score: u32,
    /// Letter grade.
    pub grade: String,
    /// Highest passing tier.
    pub certified_tier: String,
    /// Last certification timestamp.
    pub last_certified: Option<DateTime<Utc>>,
    /// Gateway 1 (load) status.
    pub g1: bool,
    /// Gateway 2 (inference) status.
    pub g2: bool,
    /// Gateway 3 (stability) status.
    pub g3: bool,
    /// Gateway 4 (quality) status.
    pub g4: bool,
    /// Throughput in tokens/sec for GGUF format (CPU).
    pub tps_gguf_cpu: Option<f64>,
    /// Throughput in tokens/sec for GGUF format (GPU).
    pub tps_gguf_gpu: Option<f64>,
    /// Throughput in tokens/sec for APR format (CPU).
    pub tps_apr_cpu: Option<f64>,
    /// Throughput in tokens/sec for APR format (GPU).
    pub tps_apr_gpu: Option<f64>,
    /// Throughput in tokens/sec for `SafeTensors` format (CPU).
    pub tps_st_cpu: Option<f64>,
    /// Throughput in tokens/sec for `SafeTensors` format (GPU).
    pub tps_st_gpu: Option<f64>,
    /// Provenance verified (PMAT-PROV-001).
    pub provenance_verified: bool,
    /// Kernel proof reference model (for dim-smoke tier).
    pub kernel_proof_ref: Option<String>,
}

impl ModelCertification {
    /// Get the short model name (without org prefix).
    #[must_use]
    pub fn short_name(&self) -> &str {
        self.model_id
            .split('/')
            .next_back()
            .unwrap_or(&self.model_id)
    }

    /// Get `HuggingFace` URL for this model.
    #[must_use]
    pub fn hf_url(&self) -> String {
        format!("https://huggingface.co/{}", self.model_id)
    }

    /// Get markdown link for this model.
    #[must_use]
    pub fn markdown_link(&self) -> String {
        format!("[{}]({})", self.short_name(), self.hf_url())
    }

    /// Get gateway symbol for display.
    #[must_use]
    pub const fn gateway_symbol(passed: bool, status: CertificationStatus) -> &'static str {
        if matches!(status, CertificationStatus::Pending) {
            "-"
        } else if passed {
            "\u{2713}" // checkmark
        } else {
            "\u{2717}" // x mark
        }
    }

    /// Parse numeric parameter count for sorting.
    #[must_use]
    pub fn param_count(&self) -> f64 {
        self.parameters
            .trim_end_matches('B')
            .parse::<f64>()
            .unwrap_or(0.0)
    }
}

/// Parse CSV content into model certifications.
///
/// # Errors
///
/// Required CSV columns — must all be present in the header.
const REQUIRED_CSV_COLUMNS: &[&str] = &[
    "model_id",
    "family",
    "parameters",
    "size_category",
    "status",
    "mqs_score",
    "grade",
    "certified_tier",
    "last_certified",
    "g1",
    "g2",
    "g3",
    "g4",
];

/// Parse a CSV certification table into a list of `ModelCertification` records.
///
/// # Errors
///
/// Returns `CertifyError::CsvParse` if the CSV header is missing required columns
/// or if any data row contains malformed field values.
#[allow(clippy::similar_names)]
pub fn parse_csv(content: &str) -> Result<Vec<ModelCertification>> {
    let mut models = Vec::new();
    let mut lines = content.lines().enumerate();

    // Parse header to build name→index map (prevents fragility from column reordering)
    let Some((_, header)) = lines.next() else {
        return Ok(models);
    };

    let header_fields = csv_split(header);
    let col: std::collections::HashMap<&str, usize> = header_fields
        .iter()
        .enumerate()
        .map(|(i, name)| (name.as_str(), i))
        .collect();

    // Fail fast if any required columns are missing
    for &name in REQUIRED_CSV_COLUMNS {
        if !col.contains_key(name) {
            return Err(CertifyError::CsvParse {
                line: 1,
                message: format!("missing required column '{name}'"),
            });
        }
    }

    for (line_num, line) in lines {
        if line.trim().is_empty() {
            continue;
        }

        let fields = csv_split(line);
        let get = |name: &str| -> &str {
            col.get(name)
                .and_then(|&i| fields.get(i).map(String::as_str))
                .unwrap_or("")
        };
        let get_opt = |name: &str| -> Option<String> {
            col.get(name)
                .and_then(|&i| fields.get(i))
                .filter(|s| !s.is_empty())
                .cloned()
        };

        let last_certified = DateTime::parse_from_rfc3339(get("last_certified"))
            .ok()
            .map(|dt| dt.with_timezone(&Utc));

        models.push(ModelCertification {
            model_id: get("model_id").to_owned(),
            family: get("family").to_owned(),
            parameters: get("parameters").to_owned(),
            size_category: SizeCategory::parse(get("size_category")).ok_or_else(|| {
                CertifyError::CsvParse {
                    line: line_num + 1,
                    message: format!("invalid size_category: '{}'", get("size_category")),
                }
            })?,
            status: CertificationStatus::parse(get("status")).ok_or_else(|| {
                CertifyError::CsvParse {
                    line: line_num + 1,
                    message: format!("invalid status: '{}'", get("status")),
                }
            })?,
            mqs_score: get("mqs_score")
                .parse()
                .map_err(|_| CertifyError::CsvParse {
                    line: line_num + 1,
                    message: format!("invalid mqs_score: '{}'", get("mqs_score")),
                })?,
            grade: get("grade").to_owned(),
            certified_tier: get("certified_tier").to_owned(),
            last_certified,
            g1: get("g1").to_lowercase() == "true",
            g2: get("g2").to_lowercase() == "true",
            g3: get("g3").to_lowercase() == "true",
            g4: get("g4").to_lowercase() == "true",
            tps_gguf_cpu: get_opt("tps_gguf_cpu").and_then(|s| s.parse().ok()),
            tps_gguf_gpu: get_opt("tps_gguf_gpu").and_then(|s| s.parse().ok()),
            tps_apr_cpu: get_opt("tps_apr_cpu").and_then(|s| s.parse().ok()),
            tps_apr_gpu: get_opt("tps_apr_gpu").and_then(|s| s.parse().ok()),
            tps_st_cpu: get_opt("tps_st_cpu").and_then(|s| s.parse().ok()),
            tps_st_gpu: get_opt("tps_st_gpu").and_then(|s| s.parse().ok()),
            provenance_verified: get_opt("provenance_verified")
                .is_some_and(|s| s.to_lowercase() == "true"),
            kernel_proof_ref: get_opt("kernel_proof_ref"),
        });
    }

    Ok(models)
}

/// Generate certification summary statistics.
#[must_use]
pub fn generate_summary(models: &[ModelCertification], timestamp: &str) -> String {
    let total = models.len();
    let certified = models
        .iter()
        .filter(|m| matches!(m.status, CertificationStatus::Certified))
        .count();
    let provisional = models
        .iter()
        .filter(|m| matches!(m.status, CertificationStatus::Provisional))
        .count();
    let blocked = models
        .iter()
        .filter(|m| matches!(m.status, CertificationStatus::Blocked))
        .count();
    let pending = models
        .iter()
        .filter(|m| matches!(m.status, CertificationStatus::Pending))
        .count();

    format!(
        r"**Certification Summary** (updated: {timestamp})

| Status | Count |
|--------|-------|
| Certified | {certified}/{total} |
| Provisional | {provisional}/{total} |
| Blocked | {blocked}/{total} |
| Pending | {pending}/{total} |

**Priority Family:** Qwen Coder (see [Certified Testing Spec](docs/specifications/certified-testing.md))"
    )
}

/// Generate markdown table from model certifications.
#[must_use]
pub fn generate_table(models: &[ModelCertification]) -> String {
    let mut lines = Vec::new();

    // Header with tok/s columns (format × backend = 6 columns) + provenance
    lines.push(
        "| Model | Family | Size | Status | MQS | Grade | G1-4 | Prov | GGUF CPU | GGUF GPU | APR CPU | APR GPU | ST CPU | ST GPU |"
            .to_string(),
    );
    lines.push(
        "|-------|--------|------|--------|-----|-------|------|------|----------|----------|---------|---------|--------|--------|"
            .to_string(),
    );

    // Sort by family, then by parameter count
    let mut sorted: Vec<_> = models.iter().collect();
    sorted.sort_by(|a, b| {
        a.family.cmp(&b.family).then_with(|| {
            a.param_count()
                .partial_cmp(&b.param_count())
                .unwrap_or(std::cmp::Ordering::Equal)
        })
    });

    for m in sorted {
        // Combine gateways into single column (all must pass)
        let gateways = if matches!(m.status, CertificationStatus::Pending) {
            "-".to_string()
        } else if m.g1 && m.g2 && m.g3 && m.g4 {
            "\u{2713}".to_string() // checkmark
        } else {
            "\u{2717}".to_string() // x mark
        };

        // Provenance status
        let prov = if matches!(m.status, CertificationStatus::Pending) {
            "-"
        } else if m.provenance_verified {
            "\u{2713}" // checkmark
        } else {
            "\u{2717}" // x mark
        };

        // Format tok/s values (6 columns)
        let fmt = |v: Option<f64>| v.map_or_else(|| "-".to_string(), |x| format!("{x:.1}"));

        lines.push(format!(
            "| {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} | {} |",
            m.markdown_link(),
            m.family,
            m.parameters,
            m.status.badge(),
            m.mqs_score,
            m.grade,
            gateways,
            prov,
            fmt(m.tps_gguf_cpu),
            fmt(m.tps_gguf_gpu),
            fmt(m.tps_apr_cpu),
            fmt(m.tps_apr_gpu),
            fmt(m.tps_st_cpu),
            fmt(m.tps_st_gpu),
        ));
    }

    lines.join("\n")
}

/// Markers for README table replacement.
pub const START_MARKER: &str = "<!-- CERTIFICATION_TABLE_START -->";
/// End marker for README table.
pub const END_MARKER: &str = "<!-- CERTIFICATION_TABLE_END -->";

/// Write certification records to CSV format.
///
/// Generates a CSV string with headers that can be written to models.csv.
#[must_use]
pub fn write_csv(models: &[ModelCertification]) -> String {
    let mut lines = Vec::new();

    // Header with 6 tps columns (format × backend) + provenance + kernel_proof_ref
    lines.push(
        "model_id,family,parameters,size_category,status,mqs_score,grade,certified_tier,last_certified,g1,g2,g3,g4,tps_gguf_cpu,tps_gguf_gpu,tps_apr_cpu,tps_apr_gpu,tps_st_cpu,tps_st_gpu,provenance_verified,kernel_proof_ref"
            .to_string(),
    );

    for m in models {
        let size_cat = match m.size_category {
            SizeCategory::Tiny => "tiny",
            SizeCategory::Small => "small",
            SizeCategory::Medium => "medium",
            SizeCategory::Large => "large",
            SizeCategory::XLarge => "xlarge",
        };
        let last_cert = m
            .last_certified
            .map_or_else(String::new, |dt| dt.to_rfc3339());

        // Format tps values (empty string for None)
        let fmt = |v: Option<f64>| v.map_or(String::new(), |x| format!("{x:.1}"));

        lines.push(format!(
            "{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}",
            csv_quote(&m.model_id),
            csv_quote(&m.family),
            csv_quote(&m.parameters),
            size_cat,
            m.status,
            m.mqs_score,
            csv_quote(&m.grade),
            csv_quote(&m.certified_tier),
            last_cert,
            m.g1,
            m.g2,
            m.g3,
            m.g4,
            fmt(m.tps_gguf_cpu),
            fmt(m.tps_gguf_gpu),
            fmt(m.tps_apr_cpu),
            fmt(m.tps_apr_gpu),
            fmt(m.tps_st_cpu),
            fmt(m.tps_st_gpu),
            m.provenance_verified,
            csv_quote(m.kernel_proof_ref.as_deref().unwrap_or("")),
        ));
    }

    lines.join("\n") + "\n"
}

/// RFC 4180 CSV quoting: wrap in double-quotes if the field contains
/// a comma, double-quote, or newline. Internal double-quotes are escaped
/// by doubling them.
fn csv_quote(field: &str) -> String {
    if field.contains(',') || field.contains('"') || field.contains('\n') || field.contains('\r') {
        format!("\"{}\"", field.replace('"', "\"\""))
    } else {
        field.to_string()
    }
}

/// RFC 4180 CSV field splitting: handles double-quoted fields containing
/// commas and escaped double-quotes (doubled `""`).
fn csv_split(line: &str) -> Vec<String> {
    let mut fields = Vec::new();
    let mut current = String::new();
    let mut in_quotes = false;
    let mut chars = line.chars().peekable();

    while let Some(ch) = chars.next() {
        match ch {
            '"' if in_quotes => {
                if chars.peek() == Some(&'"') {
                    // Escaped double-quote
                    current.push('"');
                    chars.next();
                } else {
                    in_quotes = false;
                }
            }
            '"' if !in_quotes && current.is_empty() => {
                in_quotes = true;
            }
            ',' if !in_quotes => {
                fields.push(std::mem::take(&mut current));
            }
            _ => {
                current.push(ch);
            }
        }
    }
    fields.push(current);
    fields
}

// Certification tier scoring logic
include!("lib_scoring.rs");

#[cfg(test)]
#[path = "lib_tests.rs"]
mod tests;