aprender-core 0.31.2

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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#![allow(clippy::disallowed_methods)]
//! QA Infrastructure Falsification Tests (PMAT-098 Red Team)
//!
//! This file attempts to falsify the claims made about qa_run.rs:
//! 1. Hang detection reliability
//! 2. Garbage detection completeness
//! 3. Server lifecycle cleanup
//! 4. Matrix integrity (27-test coverage)
//! 5. Answer verification brittleness
//!
//! Run with: cargo run --example qa_falsify

use std::process::{Command, Stdio};
use std::time::{Duration, Instant};

// ============================================================================
// GARBAGE PATTERNS (copied from qa_run.rs for testing)
// ============================================================================

const GARBAGE_PATTERNS: &[&str] = &[
    "\u{FFFD}", // Replacement character (encoding error)
    "[UNK]",    // Unknown token marker
    "akunji",   // Known GQA bug garbage
    "olumbia",  // Known layout bug garbage
    "专门窗",   // Known GQA bug CJK garbage
    "token0",   // Raw token ID leak
    "token1",   // Raw token ID leak
    "<0x",      // Byte token leak (e.g., <0x0A>)
];

const BPE_ARTIFACTS: &[char] = &[
    'Ġ', // GPT-2 style space prefix
    'Ċ', // GPT-2 style newline
    'ĉ', // GPT-2 style tab
];

/// Check if `needle` appears in `haystack` as a standalone word
fn contains_as_word(haystack: &str, needle: &str) -> bool {
    let mut search_start = 0;
    while let Some(pos) = haystack[search_start..].find(needle) {
        let abs_pos = search_start + pos;
        let end_pos = abs_pos + needle.len();

        let left_ok = abs_pos == 0 || {
            let prev_char = haystack[..abs_pos]
                .chars()
                .last()
                .expect("non-empty prefix must have a last char");
            !prev_char.is_alphanumeric()
        };

        let right_ok = end_pos >= haystack.len() || {
            let next_char = haystack[end_pos..]
                .chars()
                .next()
                .expect("non-empty suffix must have a next char");
            !next_char.is_alphanumeric()
        };

        if left_ok && right_ok {
            return true;
        }

        search_start = abs_pos + 1;
        if search_start >= haystack.len() {
            break;
        }
    }
    false
}

/// Replicates verify_output logic for testing (UPDATED with word boundary fix)
fn verify_output(output: &str, expected_contains: Option<&str>) -> Result<String, String> {
    let trimmed = output.trim();

    // 1. Empty check
    if trimmed.is_empty() {
        return Err("FailEmpty".to_string());
    }

    // 2. Garbage detection
    for pattern in GARBAGE_PATTERNS {
        if trimmed.contains(pattern) {
            return Err(format!("FailGarbage: {}", pattern));
        }
    }

    // 3. BPE artifact check
    for &artifact in BPE_ARTIFACTS {
        if trimmed.contains(artifact) {
            return Err(format!("FailBpeArtifact: {}", artifact));
        }
    }

    // 4. Expected answer check WITH WORD BOUNDARY (fixed)
    if let Some(expected) = expected_contains {
        if !contains_as_word(trimmed, expected) {
            return Err(format!(
                "FailMissingAnswer: expected '{}' as standalone word",
                expected
            ));
        }
    }

    Ok(trimmed.to_string())
}

// ============================================================================
// SHARED HELPERS
// ============================================================================

fn print_section_header(title: &str) {
    println!(
        "\n{}═══════════════════════════════════════════════════════════════{}",
        "\x1b[1;34m", "\x1b[0m"
    );
    println!("{}{}{}", "\x1b[1;33m", title, "\x1b[0m");
    println!(
        "{}═══════════════════════════════════════════════════════════════{}\n",
        "\x1b[1;34m", "\x1b[0m"
    );
}

/// Run a list of (input, should_pass, desc) test cases through verify_output(_, None).
/// Returns (passed, failed).
fn run_garbage_cases(cases: &[(&str, bool, &str)]) -> (u32, u32) {
    let mut passed = 0u32;
    let mut failed = 0u32;
    for &(input, should_pass, desc) in cases {
        let result = verify_output(input, None);
        let actually_passed = result.is_ok();
        if actually_passed == should_pass {
            println!("  {}✓ PASS{}: {}", "\x1b[32m", "\x1b[0m", desc);
            passed += 1;
        } else {
            println!("  {}✗ FAIL{}: {}", "\x1b[31m", "\x1b[0m", desc);
            println!(
                "    Expected: {}, Got: {:?}",
                if should_pass { "PASS" } else { "FAIL" },
                result
            );
            failed += 1;
        }
    }
    (passed, failed)
}

// ============================================================================
// TEST 2: GARBAGE DETECTION FALSIFICATION
// ============================================================================

fn test_garbage_detection() {
    print_section_header("TEST 2: GARBAGE DETECTION FALSIFICATION");

    let test_cases: Vec<(&str, bool, &str)> = vec![
        (
            "This is a token",
            true,
            "Common English word 'token' - should PASS",
        ),
        (
            "token12345",
            false,
            "Raw token ID leak (token0/token1 pattern) - should FAIL",
        ),
        (
            "The symbol Ġ is used in BPE",
            false,
            "BPE artifact Ġ - should FAIL",
        ),
        (
            "I am akunji",
            false,
            "Model collapse hallucination - should FAIL",
        ),
        (
            "Welcome to Columbia University",
            false,
            "Contains 'olumbia' - should FAIL",
        ),
        ("Hello, world!", true, "Clean output - should PASS"),
        ("The answer is 4", true, "Simple answer - should PASS"),
        (
            "token",
            true,
            "Just 'token' without number - should PASS (edge case)",
        ),
        (
            "mytoken0value",
            false,
            "token0 embedded in string - should FAIL",
        ),
        ("<0x0A>newline", false, "Byte token leak - should FAIL"),
        (
            "Test [UNK] marker",
            false,
            "Unknown token marker - should FAIL",
        ),
        ("Unicode: 专门窗 text", false, "CJK garbage - should FAIL"),
    ];

    let (passed, failed) = run_garbage_cases(&test_cases);
    println!("\n  Summary: {}/{} tests passed", passed, passed + failed);

    if failed > 0 {
        println!(
            "  {}⚠ FALSIFICATION SUCCESSFUL: Garbage detection has {} edge case failures{}",
            "\x1b[1;31m", failed, "\x1b[0m"
        );
    } else {
        println!(
            "  {}✓ Garbage detection held up under testing{}",
            "\x1b[32m", "\x1b[0m"
        );
    }
}

// ============================================================================
// TEST 5: FALSE CONFIDENCE AUDIT (Answer Verification Brittleness)
// ============================================================================

/// Evaluate a single answer-verification test case. Returns 1 if brittle, 0 otherwise.
fn eval_answer_case(input: &str, expected: Option<&str>, should_pass: bool, desc: &str) -> u32 {
    let result = verify_output(input, expected);
    let actually_passed = result.is_ok();
    let is_risk = desc.contains("RISK") || desc.contains("EDGE CASE");

    if actually_passed != should_pass {
        println!("  {}{}: {}", "\x1b[31m", "\x1b[0m", desc);
        println!(
            "    Expected: {}, Got: {:?}",
            if should_pass { "PASS" } else { "FAIL" },
            result
        );
        return 0;
    }
    if is_risk {
        println!(
            "  {}⚠ RISK{}: {} (passes but semantically wrong)",
            "\x1b[33m", "\x1b[0m", desc
        );
        return 1;
    }
    println!("  {}{}: {}", "\x1b[32m", "\x1b[0m", desc);
    0
}

fn test_answer_verification() {
    print_section_header("TEST 5: FALSE CONFIDENCE AUDIT (Answer Verification)");

    let test_cases: Vec<(&str, Option<&str>, bool, &str)> = vec![
        ("4", Some("4"), true, "Exact answer '4' - should PASS"),
        (
            "The answer is 4.",
            Some("4"),
            true,
            "Answer with context - should PASS",
        ),
        (
            "The answer is not 4, but 5.",
            Some("4"),
            true,
            "Contains standalone '4' - PASSES (acceptable)",
        ),
        ("2+2=4", Some("4"), true, "Equation format - should PASS"),
        (
            "Four",
            Some("4"),
            false,
            "Word 'Four' not digit '4' - should FAIL",
        ),
        ("5", Some("4"), false, "Wrong answer - should FAIL"),
        (
            "The result is 14",
            Some("4"),
            false,
            "FIXED: '14' no longer matches '4' - should FAIL",
        ),
        (
            "I counted 4 apples and 5 oranges",
            Some("4"),
            true,
            "Multiple numbers - should PASS",
        ),
        ("", Some("4"), false, "Empty output - should FAIL"),
        ("forty-four", Some("4"), false, "Spelled out - should FAIL"),
        (
            "answer=4",
            Some("4"),
            true,
            "'4' after '=' is standalone - should PASS",
        ),
        (
            "x4y",
            Some("4"),
            false,
            "'4' embedded in alphanumeric - should FAIL",
        ),
        (
            "4.0",
            Some("4"),
            true,
            "'4' before '.' is standalone - should PASS",
        ),
    ];

    let brittle_cases: u32 = test_cases
        .iter()
        .map(|&(input, expected, should_pass, desc)| {
            eval_answer_case(input, expected, should_pass, desc)
        })
        .sum();

    if brittle_cases > 0 {
        println!(
            "\n  {}⚠ FALSIFICATION FINDING:{} {} brittle cases remain",
            "\x1b[1;33m", "\x1b[0m", brittle_cases
        );
    } else {
        println!(
            "\n  {}✓ FIX VERIFIED:{} Word boundary check prevents false positives",
            "\x1b[32m", "\x1b[0m"
        );
        println!("    - 'The result is 14' now correctly FAILS (4 embedded in 14)");
        println!("    - 'x4y' correctly FAILS (4 embedded in alphanumeric)");
        println!("    - 'The answer is 4.' correctly PASSES (4 at word boundary)");
    }
}

// ============================================================================
// TEST 4: MATRIX INTEGRITY CHECK
// ============================================================================

/// Report the cell count found in matrix output.
fn report_cell_count(num: u32) {
    match num {
        27 => println!("  {}✓ Matrix claims 27 cells{}", "\x1b[32m", "\x1b[0m"),
        21 => println!(
            "  {}⚠ Matrix has 21 cells (not 27 as documented){}",
            "\x1b[33m", "\x1b[0m"
        ),
        _ => println!(
            "  {}⚠ Matrix has {} cells (unexpected){}",
            "\x1b[33m", num, "\x1b[0m"
        ),
    }
}

/// Search stdout+stderr for a matrix cell-count line and report it.
fn parse_matrix_output(out: &std::process::Output) {
    let stdout = String::from_utf8_lossy(&out.stdout);
    let stderr = String::from_utf8_lossy(&out.stderr);

    let line = stdout
        .lines()
        .chain(stderr.lines())
        .find(|l| l.contains("FULL MATRIX:") || (l.contains("Testing") && l.contains("cell")));

    let Some(line) = line else {
        println!("  Could not find cell count in output");
        println!("  (This test requires models to be available)");
        return;
    };

    println!("  Found: {}", line.trim());
    if let Some(num) = line.split_whitespace().find_map(|w| w.parse::<u32>().ok()) {
        report_cell_count(num);
    }
}

fn print_matrix_calculation() {
    // 3 modalities x (GGUF:3 + SafeTensors:2 + APR:2) = 3 x 7 = 21 cells
    println!("  Expected matrix calculation:");
    println!("    Modalities: Run, Chat, Serve (3)");
    println!("    Formats: GGUF, SafeTensors, APR (3)");
    println!("    Configs per format:");
    println!("      - GGUF: CPU, CPU+trace, GPU = 3 cells");
    println!("      - SafeTensors: CPU, CPU+trace = 2 cells");
    println!("      - APR: CPU, CPU+trace = 2 cells");
    println!("    Per modality: 3 + 2 + 2 = 7 cells");
    println!("    Total: 3 modalities × 7 = 21 cells");
    println!();
    println!(
        "  {}NOTE:{} Documentation was updated: 21 cells (not original 27 claim).",
        "\x1b[32m", "\x1b[0m"
    );
    println!();
    println!("  To verify, run: cargo run --example qa_run -- --full-matrix --help");
    println!("  And count 'Testing N cell(s)' in the output.");
}

fn test_matrix_integrity() {
    print_section_header("TEST 4: MATRIX INTEGRITY CHECK");
    print_matrix_calculation();

    println!("\n  Running actual matrix count...");

    let output = Command::new("cargo")
        .args([
            "run",
            "--example",
            "qa_run",
            "--release",
            "--",
            "--full-matrix",
            "--verbose",
        ])
        .stdout(Stdio::piped())
        .stderr(Stdio::piped())
        .output();

    match output {
        Ok(out) => parse_matrix_output(&out),
        Err(e) => println!("  Could not run qa_run: {}", e),
    }
}

// ============================================================================
// TEST 1: HANG DETECTION (Simulation)
// ============================================================================

include!("includes/qa_falsify_include_01.rs");