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
// SHIP-TWO-001 MODEL-2 — `dataset-thestack-python-v1` (C-DATA-THESTACK-PYTHON)
// algorithm-level PARTIAL discharge for INV-DATA-001.
//
// Contract: `contracts/dataset-thestack-python-v1.yaml` v1.0.0 PROPOSED.
// Spec: `docs/specifications/aprender-train/ship-two-models-spec.md`
// MODEL-2 corpus pipeline (§26.2), AC-SHIP2-002.
//
// ## What INV-DATA-001 says
//
// description: Every file in every output shard has an SPDX license
// identifier present in `license_whitelist.spdx_allow`.
// No file with an unknown or disallowed license reaches
// a shard.
// falsifier: Stream-scan all shards; for each record, check
// `metadata.license` against the whitelist. Any
// non-whitelist license → FAIL.
//
// ## What this file proves NOW (`PARTIAL_ALGORITHM_LEVEL`)
//
// Decision rule: given a stream-scan that produces
// (`scanned_files`, `non_whitelisted_count`), Pass iff:
//
// scanned_files > 0 AND non_whitelisted_count == 0
//
// AND `non_whitelisted_count <= scanned_files` (sanity check; counter
// arithmetic must respect the partition). Any non-zero
// non-whitelisted count → Fail (one disallowed file is enough). A
// zero-files scan → Fail (caller error: nothing was actually
// validated, and we refuse to vacuously pass an empty corpus).
/// Binary verdict for `INV-DATA-001`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DataInv001Verdict {
/// Stream-scan visited at least one file AND every visited file
/// had a whitelisted SPDX license.
Pass,
/// One or more of:
/// - `scanned_files == 0` (caller error — scan produced no work).
/// - `non_whitelisted_count > 0` (at least one disallowed license).
/// - `non_whitelisted_count > scanned_files` (counter corruption —
/// the non-whitelisted partition cannot exceed the total).
Fail,
}
/// Pure verdict function for `INV-DATA-001`.
///
/// Inputs:
/// - `scanned_files`: total file count visited by the stream-scan.
/// - `non_whitelisted_count`: number of those files whose
/// `metadata.license` was not in `license_whitelist.spdx_allow`.
///
/// Pass iff:
/// 1. `scanned_files > 0`,
/// 2. `non_whitelisted_count == 0`,
/// 3. `non_whitelisted_count <= scanned_files` (counter sanity).
///
/// Otherwise `Fail`.
///
/// # Examples
///
/// 565M-token corpus, 4M files, all whitelisted — `Pass`:
/// ```
/// use aprender::format::data_inv_001::{
/// verdict_from_license_whitelist_scan, DataInv001Verdict,
/// };
/// let v = verdict_from_license_whitelist_scan(4_000_000, 0);
/// assert_eq!(v, DataInv001Verdict::Pass);
/// ```
///
/// One disallowed file in 4M — `Fail` (one is enough):
/// ```
/// use aprender::format::data_inv_001::{
/// verdict_from_license_whitelist_scan, DataInv001Verdict,
/// };
/// let v = verdict_from_license_whitelist_scan(4_000_000, 1);
/// assert_eq!(v, DataInv001Verdict::Fail);
/// ```
#[must_use]
pub fn verdict_from_license_whitelist_scan(
scanned_files: u64,
non_whitelisted_count: u64,
) -> DataInv001Verdict {
if scanned_files == 0 {
return DataInv001Verdict::Fail;
}
if non_whitelisted_count > scanned_files {
return DataInv001Verdict::Fail;
}
if non_whitelisted_count == 0 {
DataInv001Verdict::Pass
} else {
DataInv001Verdict::Fail
}
}
#[cfg(test)]
mod tests {
use super::*;
// -------------------------------------------------------------------------
// Section 1: Pass band — clean corpora at canonical sizes.
// -------------------------------------------------------------------------
#[test]
fn pass_4m_files_zero_disallowed() {
let v = verdict_from_license_whitelist_scan(4_000_000, 0);
assert_eq!(v, DataInv001Verdict::Pass);
}
#[test]
fn pass_one_file_zero_disallowed() {
let v = verdict_from_license_whitelist_scan(1, 0);
assert_eq!(v, DataInv001Verdict::Pass);
}
#[test]
fn pass_realistic_csn_python_size() {
// CSN-Python: ~455k docs after filtering.
let v = verdict_from_license_whitelist_scan(455_000, 0);
assert_eq!(v, DataInv001Verdict::Pass);
}
#[test]
fn pass_realistic_codeparrot_size() {
// codeparrot/github-code-clean Python permissive: millions of files.
let v = verdict_from_license_whitelist_scan(8_000_000, 0);
assert_eq!(v, DataInv001Verdict::Pass);
}
// -------------------------------------------------------------------------
// Section 2: Fail band — at least one disallowed file fails the gate.
// -------------------------------------------------------------------------
#[test]
fn fail_one_disallowed_in_million() {
// Strict invariant: one disallowed license is enough to fail
// the entire corpus. No tolerance band.
let v = verdict_from_license_whitelist_scan(1_000_000, 1);
assert_eq!(
v,
DataInv001Verdict::Fail,
"one disallowed license must Fail (no tolerance)"
);
}
#[test]
fn fail_half_disallowed() {
let v = verdict_from_license_whitelist_scan(1_000_000, 500_000);
assert_eq!(v, DataInv001Verdict::Fail);
}
#[test]
fn fail_all_disallowed() {
let v = verdict_from_license_whitelist_scan(1_000_000, 1_000_000);
assert_eq!(v, DataInv001Verdict::Fail);
}
// -------------------------------------------------------------------------
// Section 3: Fail band — caller / counter errors.
// -------------------------------------------------------------------------
#[test]
fn fail_zero_scanned_files() {
// Vacuous Pass refused: scanning zero files validates nothing.
let v = verdict_from_license_whitelist_scan(0, 0);
assert_eq!(
v,
DataInv001Verdict::Fail,
"zero scanned files must Fail (vacuous Pass refused)"
);
}
#[test]
fn fail_zero_scanned_with_nonzero_disallowed() {
// Counter corruption: non-whitelisted > scanned.
let v = verdict_from_license_whitelist_scan(0, 5);
assert_eq!(v, DataInv001Verdict::Fail);
}
#[test]
fn fail_non_whitelisted_exceeds_scanned() {
// Counter corruption — partition violation.
let v = verdict_from_license_whitelist_scan(100, 101);
assert_eq!(
v,
DataInv001Verdict::Fail,
"non_whitelisted > scanned must Fail (partition violation)"
);
}
// -------------------------------------------------------------------------
// Section 4: Boundary sweep — disallowed count from 0 to scanned.
// -------------------------------------------------------------------------
#[test]
fn disallowed_sweep_at_fixed_scanned() {
let scanned = 1000_u64;
let probes: Vec<(u64, DataInv001Verdict)> = vec![
(0, DataInv001Verdict::Pass),
(1, DataInv001Verdict::Fail),
(2, DataInv001Verdict::Fail),
(10, DataInv001Verdict::Fail),
(500, DataInv001Verdict::Fail),
(999, DataInv001Verdict::Fail),
(1000, DataInv001Verdict::Fail),
(1001, DataInv001Verdict::Fail), // partition violation
];
for (disallowed, expected) in probes {
let v = verdict_from_license_whitelist_scan(scanned, disallowed);
assert_eq!(
v, expected,
"scanned={scanned} disallowed={disallowed} expected {expected:?}"
);
}
}
// -------------------------------------------------------------------------
// Section 5: Domain — strict zero-tolerance for license violations.
// -------------------------------------------------------------------------
#[test]
fn pass_iff_disallowed_is_exactly_zero() {
// Property: at any non-zero scanned count, Pass iff
// disallowed == 0. No fractional/percentage tolerance.
for scanned in [1_u64, 10, 100, 1_000, 10_000, 100_000, 1_000_000] {
let pass_v = verdict_from_license_whitelist_scan(scanned, 0);
assert_eq!(pass_v, DataInv001Verdict::Pass, "scanned={scanned}");
let fail_v = verdict_from_license_whitelist_scan(scanned, 1);
assert_eq!(
fail_v,
DataInv001Verdict::Fail,
"scanned={scanned} with one violation"
);
}
}
// -------------------------------------------------------------------------
// Section 6: Realistic scale — u64::MAX edge cases (counter overflow
// domain).
// -------------------------------------------------------------------------
#[test]
fn pass_u64_max_scanned_zero_disallowed() {
// Implausible but well-formed: huge scan, zero violations.
let v = verdict_from_license_whitelist_scan(u64::MAX, 0);
assert_eq!(v, DataInv001Verdict::Pass);
}
#[test]
fn fail_u64_max_disallowed_with_smaller_scanned() {
// Counter-rollover-style error: disallowed = u64::MAX, scanned
// = 100. Partition violated → Fail.
let v = verdict_from_license_whitelist_scan(100, u64::MAX);
assert_eq!(v, DataInv001Verdict::Fail);
}
}