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
// SHIP-TWO-001 — `apr-cli-operations-v1` algorithm-level PARTIAL
// discharge for FALSIFY-OPS-006.
//
// Contract: `contracts/apr-cli-operations-v1.yaml`.
// Spec: `docs/specifications/aprender-train/ship-two-models-spec.md`.
//
// ## What FALSIFY-OPS-006 says
//
// rule: Tokenizer encode/decode roundtrip
// prediction: Random UTF-8 strings survive tokenize/detokenize
// roundtrip
// test: Generate 1000 random UTF-8 strings, encode then decode,
// assert equality
// if_fails: Tokenizer drops or corrupts characters
//
// ## What this file proves NOW (`PARTIAL_ALGORITHM_LEVEL`)
//
// Decision rule: given (`docs_scanned`, `roundtrip_failures`),
// Pass iff:
//
// docs_scanned >= AC_OPS_006_REQUIRED_DOCS (1000) AND
// roundtrip_failures == 0 AND
// roundtrip_failures <= docs_scanned
//
// Same shape as `bpe_inv_003` (BPE round-trip) but with a smaller
// (1000-doc) sample size and broader scope (any UTF-8 input,
// not just held-out corpus).
/// Required minimum number of random UTF-8 strings to scan.
///
/// Per contract `OPS-006`: "Generate 1000 random UTF-8 strings".
/// Drift to 100 would lose statistical power for rare-codepoint
/// regressions; drift to 10000 would over-tax smoke runs.
pub const AC_OPS_006_REQUIRED_DOCS: u64 = 1_000;
/// Binary verdict for `FALSIFY-OPS-006`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Ops006Verdict {
/// Sample-scan visited >= 1000 random UTF-8 strings AND zero
/// round-trip failures observed.
Pass,
/// One or more of:
/// - `docs_scanned < 1000` (insufficient sample size).
/// - `roundtrip_failures > 0` (tokenizer dropped or corrupted
/// characters; one is enough).
/// - `roundtrip_failures > docs_scanned` (counter corruption).
Fail,
}
/// Pure verdict function for `FALSIFY-OPS-006`.
///
/// Inputs:
/// - `docs_scanned`: number of random UTF-8 strings the
/// roundtrip-test harness evaluated.
/// - `roundtrip_failures`: number of those where
/// `decode(encode(s)) != s`.
///
/// Pass iff:
/// 1. `docs_scanned >= 1000`,
/// 2. `roundtrip_failures == 0`,
/// 3. `roundtrip_failures <= docs_scanned`.
///
/// Otherwise `Fail`.
#[must_use]
pub fn verdict_from_random_roundtrip_scan(
docs_scanned: u64,
roundtrip_failures: u64,
) -> Ops006Verdict {
if docs_scanned < AC_OPS_006_REQUIRED_DOCS {
return Ops006Verdict::Fail;
}
if roundtrip_failures > docs_scanned {
return Ops006Verdict::Fail;
}
if roundtrip_failures == 0 {
Ops006Verdict::Pass
} else {
Ops006Verdict::Fail
}
}
#[cfg(test)]
mod tests {
use super::*;
// -------------------------------------------------------------------------
// Section 1: Provenance pin — 1000-doc sample floor.
// -------------------------------------------------------------------------
#[test]
fn provenance_required_docs_is_1000() {
assert_eq!(AC_OPS_006_REQUIRED_DOCS, 1_000);
}
// -------------------------------------------------------------------------
// Section 2: Pass band — clean tokenizer, sufficient sample.
// -------------------------------------------------------------------------
#[test]
fn pass_at_exact_floor() {
let v = verdict_from_random_roundtrip_scan(1_000, 0);
assert_eq!(v, Ops006Verdict::Pass);
}
#[test]
fn pass_above_floor() {
let v = verdict_from_random_roundtrip_scan(10_000, 0);
assert_eq!(v, Ops006Verdict::Pass);
}
#[test]
fn pass_at_huge_sample() {
let v = verdict_from_random_roundtrip_scan(1_000_000, 0);
assert_eq!(v, Ops006Verdict::Pass);
}
// -------------------------------------------------------------------------
// Section 3: Fail band — roundtrip failures (zero-tolerance).
// -------------------------------------------------------------------------
#[test]
fn fail_one_failure_in_1000() {
let v = verdict_from_random_roundtrip_scan(1_000, 1);
assert_eq!(
v,
Ops006Verdict::Fail,
"one roundtrip failure must Fail (no tolerance)"
);
}
#[test]
fn fail_handful_of_failures() {
let v = verdict_from_random_roundtrip_scan(1_000, 7);
assert_eq!(v, Ops006Verdict::Fail);
}
#[test]
fn fail_one_in_million() {
let v = verdict_from_random_roundtrip_scan(1_000_000, 1);
assert_eq!(v, Ops006Verdict::Fail);
}
// -------------------------------------------------------------------------
// Section 4: Fail band — sample size too small.
// -------------------------------------------------------------------------
#[test]
fn fail_zero_docs() {
let v = verdict_from_random_roundtrip_scan(0, 0);
assert_eq!(v, Ops006Verdict::Fail);
}
#[test]
fn fail_just_below_floor() {
let v = verdict_from_random_roundtrip_scan(999, 0);
assert_eq!(v, Ops006Verdict::Fail);
}
#[test]
fn fail_one_doc() {
let v = verdict_from_random_roundtrip_scan(1, 0);
assert_eq!(v, Ops006Verdict::Fail);
}
// -------------------------------------------------------------------------
// Section 5: Counter / partition violations.
// -------------------------------------------------------------------------
#[test]
fn fail_failures_exceed_docs() {
let v = verdict_from_random_roundtrip_scan(1_000, 1_001);
assert_eq!(v, Ops006Verdict::Fail);
}
#[test]
fn fail_huge_failures_with_smaller_scan() {
let v = verdict_from_random_roundtrip_scan(1_000, u64::MAX);
assert_eq!(v, Ops006Verdict::Fail);
}
// -------------------------------------------------------------------------
// Section 6: Boundary sweep.
// -------------------------------------------------------------------------
#[test]
fn failure_count_sweep_at_1000_scan() {
let scanned = 1_000_u64;
let probes: Vec<(u64, Ops006Verdict)> = vec![
(0, Ops006Verdict::Pass),
(1, Ops006Verdict::Fail),
(10, Ops006Verdict::Fail),
(500, Ops006Verdict::Fail),
(999, Ops006Verdict::Fail),
(1_000, Ops006Verdict::Fail),
(1_001, Ops006Verdict::Fail), // partition
];
for (failures, expected) in probes {
let v = verdict_from_random_roundtrip_scan(scanned, failures);
assert_eq!(
v, expected,
"scanned={scanned} failures={failures} expected {expected:?}"
);
}
}
// -------------------------------------------------------------------------
// Section 7: Domain — zero-tolerance property.
// -------------------------------------------------------------------------
#[test]
fn pass_iff_failures_is_exactly_zero() {
for scanned in [1_000_u64, 5_000, 10_000, 100_000] {
let v_pass = verdict_from_random_roundtrip_scan(scanned, 0);
assert_eq!(v_pass, Ops006Verdict::Pass, "scanned={scanned}");
let v_fail = verdict_from_random_roundtrip_scan(scanned, 1);
assert_eq!(
v_fail,
Ops006Verdict::Fail,
"scanned={scanned} with one failure"
);
}
}
}