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
// SHIP-TWO-001 — `apr-cli-qa-v1` algorithm-level PARTIAL discharge
// for FALSIFY-QA-007.
//
// Contract: `contracts/apr-cli-qa-v1.yaml`.
// Spec: `docs/specifications/aprender-train/ship-two-models-spec.md`
// (apr CLI QA gates).
//
// ## What FALSIFY-QA-007 says
//
// rule: --json flag changes output
// prediction: "json output differs from default"
// test: "diff <(apr inspect model) <(apr inspect --json model) |
// grep -q ."
// if_fails: "--json flag is a no-op"
//
// ## What this file proves NOW (`PARTIAL_ALGORITHM_LEVEL`)
//
// Decision rule: given two byte slices (`default_output`,
// `json_output`) from running the same apr command twice (once
// without --json, once with), Pass iff:
//
// default_output is non-empty AND
// json_output is non-empty AND
// default_output != json_output
//
// Inverse-equality verdict: the two outputs MUST differ. Same
// shape catches the regression where --json is parsed but doesn't
// route to a different formatter.
/// Binary verdict for `FALSIFY-QA-007`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Qa007Verdict {
/// Both outputs are non-empty AND `default_output != json_output`.
Pass,
/// One or more of:
/// - `default_output.is_empty()` (caller error — apr without
/// --json silently failed).
/// - `json_output.is_empty()` (caller error — apr with --json
/// silently failed).
/// - `default_output == json_output` (regression — --json
/// flag is a no-op; both formats route to the same writer).
Fail,
}
/// Pure verdict function for `FALSIFY-QA-007`.
///
/// Inputs:
/// - `default_output`: stdout bytes from `apr <subcmd> <args>`.
/// - `json_output`: stdout bytes from `apr <subcmd> --json <args>`.
///
/// Pass iff:
/// 1. `!default_output.is_empty()`,
/// 2. `!json_output.is_empty()`,
/// 3. `default_output != json_output`.
///
/// Otherwise `Fail`.
///
/// # Examples
///
/// Default and JSON outputs differ (typical) — `Pass`:
/// ```
/// use aprender::format::qa_007::{
/// verdict_from_json_flag_diff, Qa007Verdict,
/// };
/// let default_out = b"Model: qwen2.5-coder\nTensors: 339\n";
/// let json_out = b"{\"model\":\"qwen2.5-coder\",\"tensors\":339}\n";
/// let v = verdict_from_json_flag_diff(default_out, json_out);
/// assert_eq!(v, Qa007Verdict::Pass);
/// ```
///
/// --json flag is a no-op (regression) — `Fail`:
/// ```
/// use aprender::format::qa_007::{
/// verdict_from_json_flag_diff, Qa007Verdict,
/// };
/// let same = b"Model: qwen2.5-coder\nTensors: 339\n";
/// let v = verdict_from_json_flag_diff(same, same);
/// assert_eq!(v, Qa007Verdict::Fail);
/// ```
#[must_use]
pub fn verdict_from_json_flag_diff(
default_output: &[u8],
json_output: &[u8],
) -> Qa007Verdict {
if default_output.is_empty() || json_output.is_empty() {
return Qa007Verdict::Fail;
}
if default_output != json_output {
Qa007Verdict::Pass
} else {
Qa007Verdict::Fail
}
}
#[cfg(test)]
mod tests {
use super::*;
// -------------------------------------------------------------------------
// Section 1: Pass band — default and JSON outputs differ.
// -------------------------------------------------------------------------
#[test]
fn pass_realistic_inspect_default_vs_json() {
let default_out = b"Model: qwen2.5-coder-7b\nTensors: 339\nQuant: Q4_K\n";
let json_out = b"{\"model\":\"qwen2.5-coder-7b\",\"tensors\":339,\"quant\":\"Q4_K\"}\n";
let v = verdict_from_json_flag_diff(default_out, json_out);
assert_eq!(v, Qa007Verdict::Pass);
}
#[test]
fn pass_one_byte_difference() {
// Smallest possible difference still passes.
let default_out = b"Model: A";
let json_out = b"Model: B";
let v = verdict_from_json_flag_diff(default_out, json_out);
assert_eq!(v, Qa007Verdict::Pass);
}
#[test]
fn pass_completely_different_lengths() {
let default_out = b"short";
let json_out = b"a much longer JSON-formatted output that wraps the data";
let v = verdict_from_json_flag_diff(default_out, json_out);
assert_eq!(v, Qa007Verdict::Pass);
}
#[test]
fn pass_realistic_apr_qa_outputs() {
// `apr qa model` text vs `apr qa --json model` JSON.
let default_out = b" PASS T-001: integrity\n PASS T-002: signature\n";
let json_out = b"{\"results\":[{\"id\":\"T-001\",\"verdict\":\"PASS\"},{\"id\":\"T-002\",\"verdict\":\"PASS\"}]}\n";
let v = verdict_from_json_flag_diff(default_out, json_out);
assert_eq!(v, Qa007Verdict::Pass);
}
// -------------------------------------------------------------------------
// Section 2: Fail band — --json is a no-op (the regression).
// -------------------------------------------------------------------------
#[test]
fn fail_json_flag_no_op() {
// Both outputs identical: --json flag was parsed but
// ignored.
let same = b"Model: qwen2.5-coder\nTensors: 339\n";
let v = verdict_from_json_flag_diff(same, same);
assert_eq!(
v,
Qa007Verdict::Fail,
"--json no-op must Fail (both outputs identical)"
);
}
#[test]
fn fail_byte_identical_long_output() {
// Long but identical output.
let same = b"\
{\"model\":\"qwen2.5-coder-7b-apache-q4k-v1\",\"tensors\":339,\"layers\":28,\"hidden\":3584,\"heads\":28,\"kv_heads\":4,\"vocab\":152064}
";
let v = verdict_from_json_flag_diff(same, same);
assert_eq!(v, Qa007Verdict::Fail);
}
// -------------------------------------------------------------------------
// Section 3: Fail band — empty inputs.
// -------------------------------------------------------------------------
#[test]
fn fail_default_empty() {
let v = verdict_from_json_flag_diff(&[], b"some json");
assert_eq!(
v,
Qa007Verdict::Fail,
"empty default output must Fail (apr silent regression)"
);
}
#[test]
fn fail_json_empty() {
let v = verdict_from_json_flag_diff(b"some text", &[]);
assert_eq!(
v,
Qa007Verdict::Fail,
"empty json output must Fail"
);
}
#[test]
fn fail_both_empty() {
let v = verdict_from_json_flag_diff(&[], &[]);
assert_eq!(
v,
Qa007Verdict::Fail,
"both empty must Fail (vacuous Pass refused)"
);
}
// -------------------------------------------------------------------------
// Section 4: Edge — single-byte non-trivial differences.
// -------------------------------------------------------------------------
#[test]
fn pass_one_byte_appended_to_json() {
// JSON output has trailing newline that default lacks.
let default_out = b"output";
let json_out = b"output\n";
let v = verdict_from_json_flag_diff(default_out, json_out);
assert_eq!(v, Qa007Verdict::Pass);
}
#[test]
fn pass_one_byte_prepended() {
let default_out = b"data";
let json_out = b"{data";
let v = verdict_from_json_flag_diff(default_out, json_out);
assert_eq!(v, Qa007Verdict::Pass);
}
// -------------------------------------------------------------------------
// Section 5: Symmetry — verdict is symmetric in (default, json).
// -------------------------------------------------------------------------
#[test]
fn verdict_is_symmetric_pass() {
let a = b"text format";
let b = b"{\"json\":true}";
let ab = verdict_from_json_flag_diff(a, b);
let ba = verdict_from_json_flag_diff(b, a);
assert_eq!(ab, ba);
assert_eq!(ab, Qa007Verdict::Pass);
}
#[test]
fn verdict_is_symmetric_fail_identical() {
let same = b"identical";
let v1 = verdict_from_json_flag_diff(same, same);
let v2 = verdict_from_json_flag_diff(same, same);
assert_eq!(v1, v2);
assert_eq!(v1, Qa007Verdict::Fail);
}
#[test]
fn verdict_is_symmetric_fail_one_empty() {
let real = b"data";
let ae = verdict_from_json_flag_diff(real, &[]);
let eb = verdict_from_json_flag_diff(&[], real);
assert_eq!(ae, eb);
assert_eq!(ae, Qa007Verdict::Fail);
}
// -------------------------------------------------------------------------
// Section 6: Domain — inverse-equality property.
// -------------------------------------------------------------------------
#[test]
fn pass_iff_outputs_differ_at_canonical_lengths() {
let lengths: Vec<usize> = vec![1, 10, 100, 1000];
for len in lengths {
let a = vec![b'a'; len];
let b = vec![b'b'; len];
let v_diff = verdict_from_json_flag_diff(&a, &b);
assert_eq!(v_diff, Qa007Verdict::Pass, "len={len} differing");
let v_same = verdict_from_json_flag_diff(&a, &a);
assert_eq!(v_same, Qa007Verdict::Fail, "len={len} identical");
}
}
// -------------------------------------------------------------------------
// Section 7: Realistic — 3 apr subcommands' default vs --json contrast.
// -------------------------------------------------------------------------
#[test]
fn pass_apr_inspect_text_vs_json() {
// Per QA-007 contract test wording.
let default_out = b"Model: qwen2.5-coder-7b-apache-q4k-v1\nTensors: 339\n";
let json_out = b"{\"model\":\"qwen2.5-coder-7b-apache-q4k-v1\",\"tensors\":339}";
let v = verdict_from_json_flag_diff(default_out, json_out);
assert_eq!(v, Qa007Verdict::Pass);
}
#[test]
fn pass_apr_diff_text_vs_json() {
let default_out = b"Tensor 0: cosine 0.9999\nTensor 1: cosine 0.9998\n";
let json_out = b"{\"tensors\":[{\"idx\":0,\"cos\":0.9999},{\"idx\":1,\"cos\":0.9998}]}";
let v = verdict_from_json_flag_diff(default_out, json_out);
assert_eq!(v, Qa007Verdict::Pass);
}
#[test]
fn fail_apr_validate_json_unimplemented() {
// Hypothetical regression: `apr validate --json` was added
// to the CLI but the formatter wasn't routed; both outputs
// print the text format.
let same = b"VALIDATION: PASS\n";
let v = verdict_from_json_flag_diff(same, same);
assert_eq!(
v,
Qa007Verdict::Fail,
"--json formatter unimplemented must Fail"
);
}
}