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
// `apr-compare-hf-nonvacuous-v1` algorithm-level PARTIAL discharge for
// the 3 vacuous-truth-prevention falsifiers (0-tensor exit non-zero,
// no PASS phrase on 0 tensors, mapping diagnostic on 0 tensors).
//
// Contract: `contracts/apr-compare-hf-nonvacuous-v1.yaml`.
// Refs: paiml/aprender#621 (`compare-hf reports '✓ All tensors match'
// when it compared 0 tensors`).
//
// ## What this file proves NOW (`PARTIAL_ALGORITHM_LEVEL`)
//
// Three pure decision predicates that pin the contract's invariants:
// vacuous truth at the library level (∀ ∅ = true) MUST be guarded at
// the CLI boundary. A user running `apr compare-hf` wants to VERIFY,
// and "verified nothing" is not a successful verification.
/// PASS-phrase tokens that MUST NOT appear on 0-tensor output.
pub const AC_COMPAREHF_FORBIDDEN_PASS_PHRASES: [&str; 2] =
["all tensors match", "✓"];
/// Diagnostic-keyword set that MUST appear on 0-tensor output.
pub const AC_COMPAREHF_REQUIRED_DIAGNOSTIC_KEYWORDS: [&str; 4] =
["mapping", "no tensors matched", "0 tensors", "name-mapping"];
// =============================================================================
// FALSIFY-COMPARE-HF-001 — 0-tensor comparison exits non-zero
// =============================================================================
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NonzeroExitOnVacuousVerdict {
/// total_compared == 0 ⇒ exit_code != 0.
/// total_compared >= 1 ⇒ exit_code reflects pass/fail (any value).
Pass,
/// total_compared == 0 AND exit_code == 0 — vacuous PASS.
Fail,
}
#[must_use]
pub fn verdict_from_nonzero_exit_on_vacuous(
total_compared: u32,
exit_code: i32,
) -> NonzeroExitOnVacuousVerdict {
if total_compared == 0 && exit_code == 0 {
NonzeroExitOnVacuousVerdict::Fail
} else {
NonzeroExitOnVacuousVerdict::Pass
}
}
// =============================================================================
// FALSIFY-COMPARE-HF-002 — no PASS phrase on 0-tensor output
// =============================================================================
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NoPassPhraseOnVacuousVerdict {
/// 0-tensor case: output does NOT contain "all tensors match" / "✓".
/// Non-zero compared: any text is acceptable.
Pass,
/// 0-tensor case but PASS phrase leaked.
Fail,
}
#[must_use]
pub fn verdict_from_no_pass_phrase_on_vacuous(
total_compared: u32,
output: &str,
) -> NoPassPhraseOnVacuousVerdict {
if total_compared > 0 {
return NoPassPhraseOnVacuousVerdict::Pass;
}
let lower = output.to_lowercase();
for phrase in AC_COMPAREHF_FORBIDDEN_PASS_PHRASES {
if lower.contains(phrase) {
return NoPassPhraseOnVacuousVerdict::Fail;
}
}
NoPassPhraseOnVacuousVerdict::Pass
}
// =============================================================================
// FALSIFY-COMPARE-HF-003 — mapping diagnostic on 0-tensor output
// =============================================================================
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MappingDiagnosticVerdict {
/// 0-tensor case: output contains a mapping/zero-tensors hint.
/// Non-zero compared: any text is acceptable.
Pass,
/// 0-tensor case but no diagnostic keyword found.
Fail,
}
#[must_use]
pub fn verdict_from_mapping_diagnostic(
total_compared: u32,
output: &str,
) -> MappingDiagnosticVerdict {
if total_compared > 0 {
return MappingDiagnosticVerdict::Pass;
}
let lower = output.to_lowercase();
for kw in AC_COMPAREHF_REQUIRED_DIAGNOSTIC_KEYWORDS {
if lower.contains(kw) {
return MappingDiagnosticVerdict::Pass;
}
}
MappingDiagnosticVerdict::Fail
}
#[cfg(test)]
mod tests {
use super::*;
// -------------------------------------------------------------------------
// Section 1: Provenance pins.
// -------------------------------------------------------------------------
#[test]
fn provenance_forbidden_pass_phrases_count_2() {
assert_eq!(AC_COMPAREHF_FORBIDDEN_PASS_PHRASES.len(), 2);
}
#[test]
fn provenance_required_diagnostics_count_4() {
assert_eq!(AC_COMPAREHF_REQUIRED_DIAGNOSTIC_KEYWORDS.len(), 4);
}
// -------------------------------------------------------------------------
// Section 2: COMPARE-HF-001 non-zero exit on vacuous.
// -------------------------------------------------------------------------
#[test]
fn fc001_pass_zero_compared_nonzero_exit() {
assert_eq!(
verdict_from_nonzero_exit_on_vacuous(0, 1),
NonzeroExitOnVacuousVerdict::Pass
);
}
#[test]
fn fc001_pass_one_compared_zero_exit() {
// Non-vacuous comparison; exit 0 is fine if all passed.
assert_eq!(
verdict_from_nonzero_exit_on_vacuous(1, 0),
NonzeroExitOnVacuousVerdict::Pass
);
}
#[test]
fn fc001_pass_many_compared_failed_exit() {
// Non-vacuous comparison with some failures: non-zero exit OK.
assert_eq!(
verdict_from_nonzero_exit_on_vacuous(339, 1),
NonzeroExitOnVacuousVerdict::Pass
);
}
#[test]
fn fc001_fail_zero_compared_zero_exit() {
// The exact GH-621 regression class.
assert_eq!(
verdict_from_nonzero_exit_on_vacuous(0, 0),
NonzeroExitOnVacuousVerdict::Fail
);
}
// -------------------------------------------------------------------------
// Section 3: COMPARE-HF-002 no PASS phrase on vacuous.
// -------------------------------------------------------------------------
#[test]
fn fc002_pass_nonvacuous_with_pass_phrase() {
// Non-vacuous + "all tensors match" is correct.
let out = "Total tensors compared: 339\n✓ All tensors match within threshold!";
assert_eq!(
verdict_from_no_pass_phrase_on_vacuous(339, out),
NoPassPhraseOnVacuousVerdict::Pass
);
}
#[test]
fn fc002_pass_vacuous_with_error_text() {
// Vacuous with error text (no PASS phrase).
let out = "Total tensors compared: 0\nError: tensor name mapping failed";
assert_eq!(
verdict_from_no_pass_phrase_on_vacuous(0, out),
NoPassPhraseOnVacuousVerdict::Pass
);
}
#[test]
fn fc002_fail_vacuous_with_pass_phrase() {
// The exact regression: 0 compared but "All tensors match" emitted.
let out = "Total tensors compared: 0\n✓ All tensors match within threshold!";
assert_eq!(
verdict_from_no_pass_phrase_on_vacuous(0, out),
NoPassPhraseOnVacuousVerdict::Fail
);
}
#[test]
fn fc002_fail_vacuous_with_only_checkmark() {
let out = "Total: 0\n✓";
assert_eq!(
verdict_from_no_pass_phrase_on_vacuous(0, out),
NoPassPhraseOnVacuousVerdict::Fail
);
}
#[test]
fn fc002_fail_case_insensitive_pass_phrase() {
let out = "Total: 0\nALL TENSORS MATCH";
assert_eq!(
verdict_from_no_pass_phrase_on_vacuous(0, out),
NoPassPhraseOnVacuousVerdict::Fail
);
}
// -------------------------------------------------------------------------
// Section 4: COMPARE-HF-003 mapping diagnostic on vacuous.
// -------------------------------------------------------------------------
#[test]
fn fc003_pass_nonvacuous_no_diagnostic_required() {
let out = "Total: 339\n✓ All match";
assert_eq!(
verdict_from_mapping_diagnostic(339, out),
MappingDiagnosticVerdict::Pass
);
}
#[test]
fn fc003_pass_vacuous_with_mapping_keyword() {
let out = "Total: 0\nError: name mapping issue between GGUF and HF";
assert_eq!(
verdict_from_mapping_diagnostic(0, out),
MappingDiagnosticVerdict::Pass
);
}
#[test]
fn fc003_pass_vacuous_with_zero_tensors_keyword() {
let out = "Total: 0\nError: 0 tensors matched any name pattern";
assert_eq!(
verdict_from_mapping_diagnostic(0, out),
MappingDiagnosticVerdict::Pass
);
}
#[test]
fn fc003_pass_vacuous_with_no_tensors_matched_keyword() {
let out = "Total: 0\nNo tensors matched between local and HF.";
assert_eq!(
verdict_from_mapping_diagnostic(0, out),
MappingDiagnosticVerdict::Pass
);
}
#[test]
fn fc003_fail_vacuous_no_diagnostic() {
// The regression: 0 compared but no hint why.
let out = "Total: 0\nDone.";
assert_eq!(
verdict_from_mapping_diagnostic(0, out),
MappingDiagnosticVerdict::Fail
);
}
#[test]
fn fc003_fail_vacuous_empty_output() {
assert_eq!(
verdict_from_mapping_diagnostic(0, ""),
MappingDiagnosticVerdict::Fail
);
}
// -------------------------------------------------------------------------
// Section 5: Realistic — full healthy run + GH-621 regression.
// -------------------------------------------------------------------------
#[test]
fn realistic_healthy_compare_passes_all_3() {
// Successful 339-tensor comparison.
let out = "Total tensors compared: 339\n✓ All tensors match within threshold!";
assert_eq!(
verdict_from_nonzero_exit_on_vacuous(339, 0),
NonzeroExitOnVacuousVerdict::Pass
);
assert_eq!(
verdict_from_no_pass_phrase_on_vacuous(339, out),
NoPassPhraseOnVacuousVerdict::Pass
);
assert_eq!(
verdict_from_mapping_diagnostic(339, out),
MappingDiagnosticVerdict::Pass
);
}
#[test]
fn realistic_healthy_zero_tensor_with_diagnostic_passes_all_3() {
// Post-fix: 0 tensors with non-zero exit + diagnostic + no PASS phrase.
let out = "Total tensors compared: 0\nError: tensor name mapping returned 0 matches between local GGUF and HF.";
assert_eq!(
verdict_from_nonzero_exit_on_vacuous(0, 1),
NonzeroExitOnVacuousVerdict::Pass
);
assert_eq!(
verdict_from_no_pass_phrase_on_vacuous(0, out),
NoPassPhraseOnVacuousVerdict::Pass
);
assert_eq!(
verdict_from_mapping_diagnostic(0, out),
MappingDiagnosticVerdict::Pass
);
}
#[test]
fn realistic_pre_fix_gh_621_all_3_fail() {
// The exact GH-621 symptom string.
let out = "Total tensors compared: 0\n✓ All tensors match within threshold!";
assert_eq!(
verdict_from_nonzero_exit_on_vacuous(0, 0),
NonzeroExitOnVacuousVerdict::Fail
);
assert_eq!(
verdict_from_no_pass_phrase_on_vacuous(0, out),
NoPassPhraseOnVacuousVerdict::Fail
);
assert_eq!(
verdict_from_mapping_diagnostic(0, out),
MappingDiagnosticVerdict::Fail
);
}
}