aprender-core 0.33.0

Next-generation machine learning library in pure Rust
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
// `apr-qa-silent-fallback-v1` algorithm-level PARTIAL discharge for the
// 5 silent-fallback-injection falsifiers (truncated detection, zero-tps
// rejection, unknown-arch handling, missing-tokenizer detection, loud
// failure on bad input).
//
// Contract: `contracts/apr-qa-silent-fallback-v1.yaml`.
// Refs: GH-339 (chat template silent raw-prompt fallback), GH-336
// (benchmark silently swallowed errors), GH-337 (chat server byte-decode),
// GH-338 (probar silent metadata corruption), GH-439 (silent _ => default
// match arms at format boundaries).

/// Keywords that constitute a "loud" failure indication when the command
/// chooses to warn instead of error (legitimate fallback path per the
/// `loud_failure_on_bad_input` formula).
pub const AC_QASILENT_LOUD_KEYWORDS: [&str; 3] = ["WARN", "SKIP", "unsupported"];

/// Keywords that satisfy the truncation detection error message.
pub const AC_QASILENT_TRUNCATION_KEYWORDS: [&str; 3] = ["truncat", "corrupt", "incomplete"];

/// Keywords that satisfy the missing-tokenizer detection (either
/// declares failure OR notes legitimate fallback path).
pub const AC_QASILENT_TOKENIZER_KEYWORDS: [&str; 3] = ["tokenizer", "embedded", "GGUF"];

/// Keywords that satisfy the unknown-arch error message.
pub const AC_QASILENT_UNKNOWN_ARCH_KEYWORDS: [&str; 2] = ["unsupported", "unknown"];

// =============================================================================
// F-SILENT-001 — truncated file detection
// =============================================================================

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TruncationDetectionVerdict {
    /// `apr validate` exits non-zero AND stderr mentions truncation/corruption.
    Pass,
    /// Silent acceptance, OR non-zero exit without explanatory stderr.
    Fail,
}

#[must_use]
pub fn verdict_from_truncation_detection(exit_code: i32, stderr: &str) -> TruncationDetectionVerdict {
    if exit_code == 0 {
        return TruncationDetectionVerdict::Fail;
    }
    let lower = stderr.to_lowercase();
    for kw in AC_QASILENT_TRUNCATION_KEYWORDS {
        if lower.contains(kw) {
            return TruncationDetectionVerdict::Pass;
        }
    }
    TruncationDetectionVerdict::Fail
}

// =============================================================================
// F-SILENT-002 — zero throughput rejection
// =============================================================================

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ZeroThroughputVerdict {
    /// `apr bench` reporting tok/s == 0.0 ⇒ exit_code != 0.
    Pass,
    /// Reported 0.0 tok/s but exited 0 — the regression class.
    Fail,
}

#[must_use]
pub fn verdict_from_zero_throughput(reported_tps: f64, exit_code: i32) -> ZeroThroughputVerdict {
    if reported_tps > 0.0 {
        // Non-zero throughput is fine regardless of exit code; this gate
        // only catches the zero-as-success regression class.
        return ZeroThroughputVerdict::Pass;
    }
    // tps == 0.0 (or negative — also nonsense): require non-zero exit.
    if exit_code == 0 {
        ZeroThroughputVerdict::Fail
    } else {
        ZeroThroughputVerdict::Pass
    }
}

// =============================================================================
// F-SILENT-003 — unknown architecture handling
// =============================================================================

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum UnknownArchVerdict {
    /// Unknown architecture identifier ⇒ non-zero exit AND stderr
    /// mentions "unsupported" or "unknown".
    Pass,
    /// Silent fallback to llama default — the regression class.
    Fail,
}

#[must_use]
pub fn verdict_from_unknown_arch(exit_code: i32, stderr: &str) -> UnknownArchVerdict {
    if exit_code == 0 {
        return UnknownArchVerdict::Fail;
    }
    let lower = stderr.to_lowercase();
    for kw in AC_QASILENT_UNKNOWN_ARCH_KEYWORDS {
        if lower.contains(kw) {
            return UnknownArchVerdict::Pass;
        }
    }
    UnknownArchVerdict::Fail
}

// =============================================================================
// F-SILENT-004 — missing tokenizer detection
// =============================================================================

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MissingTokenizerVerdict {
    /// Either non-zero exit with "tokenizer" in stderr, OR uses embedded
    /// GGUF tokenizer (legitimate fallback). Never silent-garble.
    Pass,
    /// Silent garble — output emitted without explanation.
    Fail,
}

#[must_use]
pub fn verdict_from_missing_tokenizer(stderr_or_log: &str) -> MissingTokenizerVerdict {
    // The contract test is `grep -qE 'tokenizer|embedded|GGUF'`. Any of
    // these keywords appearing means the tool announced its tokenizer
    // path (either error or legitimate fallback). Absence ⇒ silent.
    for kw in AC_QASILENT_TOKENIZER_KEYWORDS {
        if stderr_or_log.contains(kw) {
            return MissingTokenizerVerdict::Pass;
        }
    }
    MissingTokenizerVerdict::Fail
}

// =============================================================================
// F-SILENT-005 — loud failure on bad input
// =============================================================================

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LoudFailureVerdict {
    /// Either: non-zero exit with non-empty stderr, OR stdout contains
    /// WARN / SKIP / unsupported.
    Pass,
    /// Silent degradation — exit 0, no stderr, no warning keywords.
    Fail,
}

#[must_use]
pub fn verdict_from_loud_failure(exit_code: i32, stderr: &str, stdout: &str) -> LoudFailureVerdict {
    // Path 1: explicit error.
    if exit_code != 0 && !stderr.is_empty() {
        return LoudFailureVerdict::Pass;
    }
    // Path 2: warning emitted on stdout.
    for kw in AC_QASILENT_LOUD_KEYWORDS {
        if stdout.contains(kw) {
            return LoudFailureVerdict::Pass;
        }
    }
    LoudFailureVerdict::Fail
}

#[cfg(test)]
mod tests {
    use super::*;

    // -------------------------------------------------------------------------
    // Section 1: Provenance pins.
    // -------------------------------------------------------------------------
    #[test]
    fn provenance_loud_keywords_count_3() {
        assert_eq!(AC_QASILENT_LOUD_KEYWORDS.len(), 3);
    }

    #[test]
    fn provenance_truncation_keywords_count_3() {
        assert_eq!(AC_QASILENT_TRUNCATION_KEYWORDS.len(), 3);
    }

    #[test]
    fn provenance_tokenizer_keywords_count_3() {
        assert_eq!(AC_QASILENT_TOKENIZER_KEYWORDS.len(), 3);
    }

    #[test]
    fn provenance_unknown_arch_keywords_count_2() {
        assert_eq!(AC_QASILENT_UNKNOWN_ARCH_KEYWORDS.len(), 2);
    }

    // -------------------------------------------------------------------------
    // Section 2: F-SILENT-001 truncation detection.
    // -------------------------------------------------------------------------
    #[test]
    fn fs001_pass_truncated_keyword() {
        let v = verdict_from_truncation_detection(1, "Error: file truncated at offset 1024");
        assert_eq!(v, TruncationDetectionVerdict::Pass);
    }

    #[test]
    fn fs001_pass_corrupt_keyword() {
        let v = verdict_from_truncation_detection(2, "Error: corrupt header magic");
        assert_eq!(v, TruncationDetectionVerdict::Pass);
    }

    #[test]
    fn fs001_pass_incomplete_keyword() {
        let v = verdict_from_truncation_detection(1, "Error: model file is incomplete");
        assert_eq!(v, TruncationDetectionVerdict::Pass);
    }

    #[test]
    fn fs001_fail_silent_zero_exit() {
        let v = verdict_from_truncation_detection(0, "model truncated");
        assert_eq!(v, TruncationDetectionVerdict::Fail);
    }

    #[test]
    fn fs001_fail_nonzero_no_keyword() {
        let v = verdict_from_truncation_detection(1, "Generic I/O error");
        assert_eq!(v, TruncationDetectionVerdict::Fail);
    }

    // -------------------------------------------------------------------------
    // Section 3: F-SILENT-002 zero throughput.
    // -------------------------------------------------------------------------
    #[test]
    fn fs002_pass_positive_tps() {
        // Non-zero throughput passes regardless of exit code.
        assert_eq!(verdict_from_zero_throughput(127.5, 0), ZeroThroughputVerdict::Pass);
    }

    #[test]
    fn fs002_pass_zero_tps_nonzero_exit() {
        assert_eq!(verdict_from_zero_throughput(0.0, 1), ZeroThroughputVerdict::Pass);
    }

    #[test]
    fn fs002_fail_zero_tps_zero_exit() {
        // The exact regression class: 0.0 tok/s reported as success.
        assert_eq!(verdict_from_zero_throughput(0.0, 0), ZeroThroughputVerdict::Fail);
    }

    #[test]
    fn fs002_fail_negative_tps_zero_exit() {
        // Negative tps is nonsense; treated like zero.
        assert_eq!(verdict_from_zero_throughput(-1.0, 0), ZeroThroughputVerdict::Fail);
    }

    // -------------------------------------------------------------------------
    // Section 4: F-SILENT-003 unknown architecture.
    // -------------------------------------------------------------------------
    #[test]
    fn fs003_pass_unsupported_keyword() {
        let v = verdict_from_unknown_arch(1, "Error: unsupported architecture totally_unknown_arch_v99");
        assert_eq!(v, UnknownArchVerdict::Pass);
    }

    #[test]
    fn fs003_pass_unknown_keyword() {
        let v = verdict_from_unknown_arch(1, "Error: unknown model arch");
        assert_eq!(v, UnknownArchVerdict::Pass);
    }

    #[test]
    fn fs003_fail_silent_zero_exit() {
        // The regression: silently fell through to llama.
        let v = verdict_from_unknown_arch(0, "");
        assert_eq!(v, UnknownArchVerdict::Fail);
    }

    #[test]
    fn fs003_fail_nonzero_no_keyword() {
        let v = verdict_from_unknown_arch(1, "Some other error");
        assert_eq!(v, UnknownArchVerdict::Fail);
    }

    // -------------------------------------------------------------------------
    // Section 5: F-SILENT-004 missing tokenizer.
    // -------------------------------------------------------------------------
    #[test]
    fn fs004_pass_tokenizer_keyword() {
        let v = verdict_from_missing_tokenizer("Error: tokenizer.json not found");
        assert_eq!(v, MissingTokenizerVerdict::Pass);
    }

    #[test]
    fn fs004_pass_embedded_fallback() {
        let v = verdict_from_missing_tokenizer("Notice: using embedded GGUF tokenizer");
        assert_eq!(v, MissingTokenizerVerdict::Pass);
    }

    #[test]
    fn fs004_pass_gguf_keyword() {
        let v = verdict_from_missing_tokenizer("Falling back to GGUF-internal vocabulary");
        assert_eq!(v, MissingTokenizerVerdict::Pass);
    }

    #[test]
    fn fs004_fail_silent_garble() {
        // Output emitted without any tokenizer/embedded/GGUF announcement.
        let v = verdict_from_missing_tokenizer("Output: ?�?�?");
        assert_eq!(v, MissingTokenizerVerdict::Fail);
    }

    #[test]
    fn fs004_fail_empty_log() {
        let v = verdict_from_missing_tokenizer("");
        assert_eq!(v, MissingTokenizerVerdict::Fail);
    }

    // -------------------------------------------------------------------------
    // Section 6: F-SILENT-005 loud failure.
    // -------------------------------------------------------------------------
    #[test]
    fn fs005_pass_explicit_error() {
        let v = verdict_from_loud_failure(1, "Error: bad metadata", "");
        assert_eq!(v, LoudFailureVerdict::Pass);
    }

    #[test]
    fn fs005_pass_warn_on_stdout() {
        let v = verdict_from_loud_failure(0, "", "WARN: degraded mode");
        assert_eq!(v, LoudFailureVerdict::Pass);
    }

    #[test]
    fn fs005_pass_skip_on_stdout() {
        let v = verdict_from_loud_failure(0, "", "SKIP: unsupported feature");
        assert_eq!(v, LoudFailureVerdict::Pass);
    }

    #[test]
    fn fs005_pass_unsupported_on_stdout() {
        let v = verdict_from_loud_failure(0, "", "Notice: unsupported quant — using f16");
        assert_eq!(v, LoudFailureVerdict::Pass);
    }

    #[test]
    fn fs005_fail_silent_success() {
        // The exact regression: bad input but exit 0, empty stderr, no warning.
        let v = verdict_from_loud_failure(0, "", "Output: garbage");
        assert_eq!(v, LoudFailureVerdict::Fail);
    }

    #[test]
    fn fs005_fail_nonzero_exit_no_stderr() {
        // Exit 1 alone isn't enough — need either stderr OR stdout warning.
        let v = verdict_from_loud_failure(1, "", "");
        assert_eq!(v, LoudFailureVerdict::Fail);
    }

    // -------------------------------------------------------------------------
    // Section 7: Realistic — full healthy bad-input run + pre-fix.
    // -------------------------------------------------------------------------
    #[test]
    fn realistic_loud_run_passes_all_5() {
        // Truncated file → non-zero exit + corrupt keyword.
        assert_eq!(
            verdict_from_truncation_detection(1, "Error: file is corrupt"),
            TruncationDetectionVerdict::Pass
        );
        // Non-zero throughput.
        assert_eq!(verdict_from_zero_throughput(150.0, 0), ZeroThroughputVerdict::Pass);
        // Unknown arch → non-zero exit + unsupported.
        assert_eq!(
            verdict_from_unknown_arch(1, "Error: unsupported architecture"),
            UnknownArchVerdict::Pass
        );
        // Missing tokenizer → embedded fallback announced.
        assert_eq!(
            verdict_from_missing_tokenizer("Notice: using embedded tokenizer"),
            MissingTokenizerVerdict::Pass
        );
        // Bad input → loud error.
        assert_eq!(
            verdict_from_loud_failure(1, "Error: bad input", ""),
            LoudFailureVerdict::Pass
        );
    }

    #[test]
    fn realistic_pre_fix_all_5_failures() {
        // 001: silent acceptance of truncated file.
        assert_eq!(
            verdict_from_truncation_detection(0, ""),
            TruncationDetectionVerdict::Fail
        );
        // 002: GH-336 — 0 tok/s reported as success.
        assert_eq!(verdict_from_zero_throughput(0.0, 0), ZeroThroughputVerdict::Fail);
        // 003: GH-439 — unknown arch silently maps to llama.
        assert_eq!(
            verdict_from_unknown_arch(0, "tokens generated successfully"),
            UnknownArchVerdict::Fail
        );
        // 004: GH-337 — chat server degrades to byte-level decode silently.
        assert_eq!(
            verdict_from_missing_tokenizer("Output: ?\u{fffd}?\u{fffd}?"),
            MissingTokenizerVerdict::Fail
        );
        // 005: GH-339 / GH-338 — silent fallback to raw prompt / accepted corruption.
        assert_eq!(
            verdict_from_loud_failure(0, "", "Output: degraded silently"),
            LoudFailureVerdict::Fail
        );
    }
}