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
// SHIP-TWO-001 MODEL-2 — `dataset-thestack-python-v1` (C-DATA-THESTACK-PYTHON)
// algorithm-level PARTIAL discharge for INV-DATA-006.
//
// Contract: `contracts/dataset-thestack-python-v1.yaml` v1.0.0 PROPOSED.
// Sibling to [`super::data_inv_004`] (#1146, train range + val floor).
//
// ## What INV-DATA-006 says
//
// description: Train and val splits are DISJOINT by file sha256. No
// file appears in both train and val shards.
//
// ## What this file proves NOW (`PARTIAL_ALGORITHM_LEVEL`)
//
// Decision rule: given two sorted lists of file SHA-256s (one for train
// shards, one for val shards), Pass iff:
//
// 1. Both sets are non-empty.
// 2. Both sets are internally deduplicated (a file MUST NOT appear
// twice in the same split — a subtler bug class than cross-split
// leakage but symptomatically similar).
// 3. The intersection of train and val is empty.
//
// Composes with [`super::ship_010::verdict_from_sha256_match`] for hash
// format validation: every hash in either list is a 64-char lowercase
// hex string.
//
// Future implementations (the actual `apr ingest` shard splitter) cannot:
// - Emit train AND val with overlapping file sha256s (eval-set leakage).
// - Emit duplicate files within a single split (also a leakage class).
// - Emit empty train OR empty val (no work done).
use super::ship_010::{verdict_from_sha256_match, Ship010Verdict};
/// Binary verdict for `INV-DATA-006`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DataInv006Verdict {
/// `train_file_sha256s ∩ val_file_sha256s = ∅`. Splits are disjoint
/// by file SHA-256, both non-empty, both internally deduplicated,
/// every hash canonical lowercase 64-char hex.
Pass,
/// One or more of:
/// - Either split is empty (caller error).
/// - Some hash is malformed (delegated to ship_010 → Fail).
/// - Train has a duplicate within itself.
/// - Val has a duplicate within itself.
/// - At least one file SHA-256 appears in both train and val.
Fail,
}
/// Pure verdict function for `INV-DATA-006`.
///
/// Inputs:
/// - `train_file_sha256s`: per-file SHA-256 hex strings for train-split files.
/// - `val_file_sha256s`: per-file SHA-256 hex strings for val-split files.
///
/// Both slices are expected to be in canonical lowercase hex.
///
/// Pass iff:
/// 1. Both slices are non-empty.
/// 2. Every hash is a valid SHA-256 (composes with [`super::ship_010`]).
/// 3. No hash appears more than once within `train_file_sha256s`.
/// 4. No hash appears more than once within `val_file_sha256s`.
/// 5. No hash appears in both `train_file_sha256s` and `val_file_sha256s`.
///
/// Otherwise `Fail`.
///
/// # Examples
///
/// Disjoint splits — `Pass`:
/// ```
/// use aprender::format::data_inv_006::{
/// verdict_from_split_file_sha256s, DataInv006Verdict,
/// };
/// let train = vec!["0".repeat(64), "1".repeat(64), "2".repeat(64)];
/// let val = vec!["a".repeat(64), "b".repeat(64)];
/// assert_eq!(
/// verdict_from_split_file_sha256s(&train, &val),
/// DataInv006Verdict::Pass,
/// );
/// ```
///
/// Eval-set leakage — `Fail`:
/// ```
/// use aprender::format::data_inv_006::{
/// verdict_from_split_file_sha256s, DataInv006Verdict,
/// };
/// let leaked = "0".repeat(64);
/// let train = vec!["1".repeat(64), leaked.clone(), "2".repeat(64)];
/// let val = vec![leaked, "a".repeat(64)];
/// assert_eq!(
/// verdict_from_split_file_sha256s(&train, &val),
/// DataInv006Verdict::Fail,
/// );
/// ```
#[must_use]
pub fn verdict_from_split_file_sha256s(
train_file_sha256s: &[String],
val_file_sha256s: &[String],
) -> DataInv006Verdict {
if train_file_sha256s.is_empty() || val_file_sha256s.is_empty() {
return DataInv006Verdict::Fail;
}
// Format-validate every hash via ship_010 (single source of truth).
for h in train_file_sha256s.iter().chain(val_file_sha256s.iter()) {
if matches!(verdict_from_sha256_match(h, h), Ship010Verdict::Fail) {
return DataInv006Verdict::Fail;
}
}
// Internal-duplicate detection within each split.
if has_internal_duplicate(train_file_sha256s) {
return DataInv006Verdict::Fail;
}
if has_internal_duplicate(val_file_sha256s) {
return DataInv006Verdict::Fail;
}
// Cross-split intersection check (the load-bearing leakage rule).
let train_set: std::collections::HashSet<&String> = train_file_sha256s.iter().collect();
for v in val_file_sha256s {
if train_set.contains(v) {
return DataInv006Verdict::Fail;
}
}
DataInv006Verdict::Pass
}
fn has_internal_duplicate(hashes: &[String]) -> bool {
let mut seen = std::collections::HashSet::with_capacity(hashes.len());
for h in hashes {
if !seen.insert(h.as_str()) {
return true;
}
}
false
}
#[cfg(test)]
mod tests {
use super::*;
fn h(c: char) -> String {
std::iter::repeat(c).take(64).collect()
}
// -------------------------------------------------------------------------
// Section 1: Pass band — well-formed disjoint splits.
// -------------------------------------------------------------------------
#[test]
fn pass_disjoint_splits() {
let train = vec![h('0'), h('1'), h('2')];
let val = vec![h('a'), h('b')];
assert_eq!(
verdict_from_split_file_sha256s(&train, &val),
DataInv006Verdict::Pass
);
}
#[test]
fn pass_minimal_each_one_file() {
let train = vec![h('0')];
let val = vec![h('a')];
assert_eq!(
verdict_from_split_file_sha256s(&train, &val),
DataInv006Verdict::Pass
);
}
#[test]
fn pass_realistic_scale_1k_train_50_val() {
// ~95/5 split at 1050 files total.
let train: Vec<String> = (0..1000).map(|i| format!("{:064x}", i)).collect();
let val: Vec<String> = (1000..1050).map(|i| format!("{:064x}", i)).collect();
assert_eq!(
verdict_from_split_file_sha256s(&train, &val),
DataInv006Verdict::Pass
);
}
// -------------------------------------------------------------------------
// Section 2: Fail band — eval-set leakage (intersection non-empty).
// -------------------------------------------------------------------------
#[test]
fn fail_one_file_in_both_splits() {
let leaked = h('0');
let train = vec![h('1'), leaked.clone(), h('2')];
let val = vec![leaked, h('a')];
assert_eq!(
verdict_from_split_file_sha256s(&train, &val),
DataInv006Verdict::Fail
);
}
#[test]
fn fail_all_files_in_both_splits() {
// Identical sets — total leakage.
let same = vec![h('0'), h('1'), h('2')];
assert_eq!(
verdict_from_split_file_sha256s(&same, &same),
DataInv006Verdict::Fail
);
}
#[test]
fn fail_leakage_at_each_position() {
for bad_idx in [0_usize, 2, 4] {
let leaked = h('f');
let mut train = vec![h('0'), h('1'), h('2'), h('3'), h('4')];
train[bad_idx] = leaked.clone();
let val = vec![leaked, h('a')];
assert_eq!(
verdict_from_split_file_sha256s(&train, &val),
DataInv006Verdict::Fail,
"leakage at index {bad_idx} must Fail"
);
}
}
// -------------------------------------------------------------------------
// Section 3: Fail band — internal duplicates within a single split.
// -------------------------------------------------------------------------
#[test]
fn fail_train_internal_duplicate() {
let train = vec![h('0'), h('1'), h('0')]; // h('0') twice
let val = vec![h('a')];
assert_eq!(
verdict_from_split_file_sha256s(&train, &val),
DataInv006Verdict::Fail
);
}
#[test]
fn fail_val_internal_duplicate() {
let train = vec![h('0')];
let val = vec![h('a'), h('a')]; // h('a') twice
assert_eq!(
verdict_from_split_file_sha256s(&train, &val),
DataInv006Verdict::Fail
);
}
// -------------------------------------------------------------------------
// Section 4: Fail band — empty splits.
// -------------------------------------------------------------------------
#[test]
fn fail_empty_train() {
let train: Vec<String> = vec![];
let val = vec![h('a')];
assert_eq!(
verdict_from_split_file_sha256s(&train, &val),
DataInv006Verdict::Fail
);
}
#[test]
fn fail_empty_val() {
let train = vec![h('0')];
let val: Vec<String> = vec![];
assert_eq!(
verdict_from_split_file_sha256s(&train, &val),
DataInv006Verdict::Fail
);
}
#[test]
fn fail_both_empty() {
let train: Vec<String> = vec![];
let val: Vec<String> = vec![];
assert_eq!(
verdict_from_split_file_sha256s(&train, &val),
DataInv006Verdict::Fail
);
}
// -------------------------------------------------------------------------
// Section 5: Fail band — format violations (delegated to ship_010).
// -------------------------------------------------------------------------
#[test]
fn fail_uppercase_hex_in_train() {
let train = vec!["A".repeat(64)];
let val = vec![h('a')];
assert_eq!(
verdict_from_split_file_sha256s(&train, &val),
DataInv006Verdict::Fail
);
}
#[test]
fn fail_too_short_hash_in_val() {
let train = vec![h('0')];
let val = vec!["a".repeat(63)];
assert_eq!(
verdict_from_split_file_sha256s(&train, &val),
DataInv006Verdict::Fail
);
}
#[test]
fn fail_non_hex_character() {
let mut bad = "0".repeat(63);
bad.push('z');
let train = vec![bad];
let val = vec![h('a')];
assert_eq!(
verdict_from_split_file_sha256s(&train, &val),
DataInv006Verdict::Fail
);
}
// -------------------------------------------------------------------------
// Section 6: Symmetry — swap train/val should still detect leakage.
// -------------------------------------------------------------------------
#[test]
fn symmetry_under_swap() {
let train = vec![h('0'), h('1'), h('2')];
let val = vec![h('a'), h('b')];
let v1 = verdict_from_split_file_sha256s(&train, &val);
let v2 = verdict_from_split_file_sha256s(&val, &train);
assert_eq!(v1, v2, "verdict must be symmetric under split swap");
assert_eq!(v1, DataInv006Verdict::Pass);
}
#[test]
fn symmetry_leakage_under_swap() {
let leaked = h('f');
let a = vec![h('0'), leaked.clone()];
let b = vec![leaked, h('a')];
assert_eq!(
verdict_from_split_file_sha256s(&a, &b),
DataInv006Verdict::Fail
);
assert_eq!(
verdict_from_split_file_sha256s(&b, &a),
DataInv006Verdict::Fail
);
}
// -------------------------------------------------------------------------
// Section 7: Internal-duplicate helper — direct test of the private fn.
// -------------------------------------------------------------------------
#[test]
fn helper_has_internal_duplicate_detects() {
let dupes = vec![h('0'), h('1'), h('0')];
assert!(has_internal_duplicate(&dupes));
}
#[test]
fn helper_has_internal_duplicate_clean_returns_false() {
let clean = vec![h('0'), h('1'), h('2')];
assert!(!has_internal_duplicate(&clean));
}
#[test]
fn helper_has_internal_duplicate_empty_returns_false() {
let empty: Vec<String> = vec![];
assert!(!has_internal_duplicate(&empty));
}
}