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
// `apr-corpus-*` family algorithm-level PARTIAL discharge for the 5
// falsification conditions shared by every `apr-corpus-*-v1.yaml` contract.
//
// Contracts: `contracts/apr-corpus-*.yaml` (13 contracts × 5 conditions =
// 65 falsifier instantiations).
//
// All apr-corpus-* contracts (vllm, jax, hugging-face, lean, ludwig, tgi,
// databricks, etc.) share an identical schema. Each declares 5
// falsification conditions:
//
// 1. "Repository contains zero test files" (P0 — add_tests)
// 2. "No Rust equivalent mapping documented" (P1 — add_mapping)
// 3. "Corpus not indexed in oracle RAG" (P1 — reindex)
// 4. "Last commit older than 180 days (staleness)" (P1 — refresh_or_archive)
// 5. "Ground-truth pattern contradicts current aprender API" (P0 — update_pattern)
//
// One verdict module satisfies the algorithm-level pin for all 13
// apr-corpus contracts. Live discharge is `apr oracle --rag-index` +
// `gh repo` queries; this module pins the predicates so future bulk
// corpus operations cannot drift on the shape of any of the 5
// invariants.
//
// Local IDs FALSIFY-APRCORPUS-001..005 are synthesized to give each
// unkeyed YAML entry a stable handle.
/// Staleness threshold per FALSIFY-APRCORPUS-004.
pub const AC_APRCORPUS_STALENESS_DAYS: u32 = 180;
/// Minimum test file count per FALSIFY-APRCORPUS-001 (the contract test
/// is "Repository contains zero test files" — exactly 0 fails).
pub const AC_APRCORPUS_MIN_TEST_FILES: u32 = 1;
// =============================================================================
// FALSIFY-APRCORPUS-001 — repository has at least 1 test file
// =============================================================================
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CorpusTestFilesVerdict {
/// At least 1 test file present.
Pass,
/// Zero test files (P0 regression).
Fail,
}
#[must_use]
pub fn verdict_from_corpus_test_files(test_file_count: u32) -> CorpusTestFilesVerdict {
if test_file_count >= AC_APRCORPUS_MIN_TEST_FILES {
CorpusTestFilesVerdict::Pass
} else {
CorpusTestFilesVerdict::Fail
}
}
// =============================================================================
// FALSIFY-APRCORPUS-002 — Rust equivalent mapping documented
// =============================================================================
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RustMappingVerdict {
/// Mapping documented (e.g., README has a "Rust equivalent" / "aprender
/// mapping" section, or every pattern file declares its Rust analogue).
Pass,
/// No mapping documented (P1).
Fail,
}
#[must_use]
pub fn verdict_from_rust_mapping(mapping_documented: bool) -> RustMappingVerdict {
if mapping_documented {
RustMappingVerdict::Pass
} else {
RustMappingVerdict::Fail
}
}
// =============================================================================
// FALSIFY-APRCORPUS-003 — corpus indexed in oracle RAG
// =============================================================================
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RagIndexedVerdict {
/// `apr oracle --rag` finds at least 1 pattern in the corpus's domain.
Pass,
/// Domain query returns zero matches (P1).
Fail,
}
#[must_use]
pub fn verdict_from_rag_indexed(rag_match_count: u32) -> RagIndexedVerdict {
if rag_match_count >= 1 {
RagIndexedVerdict::Pass
} else {
RagIndexedVerdict::Fail
}
}
// =============================================================================
// FALSIFY-APRCORPUS-004 — last commit within 180 days
// =============================================================================
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CorpusFreshnessVerdict {
/// Last commit ≤ 180 days ago.
Pass,
/// Last commit > 180 days ago (P1 — refresh_or_archive).
Fail,
}
#[must_use]
pub fn verdict_from_corpus_freshness(days_since_last_commit: u32) -> CorpusFreshnessVerdict {
if days_since_last_commit <= AC_APRCORPUS_STALENESS_DAYS {
CorpusFreshnessVerdict::Pass
} else {
CorpusFreshnessVerdict::Fail
}
}
// =============================================================================
// FALSIFY-APRCORPUS-005 — ground-truth pattern doesn't contradict aprender API
// =============================================================================
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ApiConsistencyVerdict {
/// All ground-truth patterns reference current aprender API surface.
Pass,
/// At least one pattern references removed/renamed API (P0 — update_pattern).
Fail,
}
#[must_use]
pub fn verdict_from_api_consistency(contradicting_pattern_count: u32) -> ApiConsistencyVerdict {
if contradicting_pattern_count == 0 {
ApiConsistencyVerdict::Pass
} else {
ApiConsistencyVerdict::Fail
}
}
#[cfg(test)]
mod tests {
use super::*;
// -------------------------------------------------------------------------
// Section 1: Provenance pins.
// -------------------------------------------------------------------------
#[test]
fn provenance_staleness_180_days() {
assert_eq!(AC_APRCORPUS_STALENESS_DAYS, 180);
}
#[test]
fn provenance_min_test_files_is_1() {
// Contract gate is "Repository contains ZERO test files" → fail.
// Pass requires ≥ 1.
assert_eq!(AC_APRCORPUS_MIN_TEST_FILES, 1);
}
// -------------------------------------------------------------------------
// Section 2: APRCORPUS-001 test files.
// -------------------------------------------------------------------------
#[test]
fn ac001_pass_one_test_file() {
assert_eq!(verdict_from_corpus_test_files(1), CorpusTestFilesVerdict::Pass);
}
#[test]
fn ac001_pass_many_test_files() {
assert_eq!(verdict_from_corpus_test_files(100), CorpusTestFilesVerdict::Pass);
}
#[test]
fn ac001_fail_zero_test_files() {
assert_eq!(verdict_from_corpus_test_files(0), CorpusTestFilesVerdict::Fail);
}
// -------------------------------------------------------------------------
// Section 3: APRCORPUS-002 Rust mapping.
// -------------------------------------------------------------------------
#[test]
fn ac002_pass_mapping_documented() {
assert_eq!(verdict_from_rust_mapping(true), RustMappingVerdict::Pass);
}
#[test]
fn ac002_fail_no_mapping() {
assert_eq!(verdict_from_rust_mapping(false), RustMappingVerdict::Fail);
}
// -------------------------------------------------------------------------
// Section 4: APRCORPUS-003 RAG indexed.
// -------------------------------------------------------------------------
#[test]
fn ac003_pass_one_match() {
assert_eq!(verdict_from_rag_indexed(1), RagIndexedVerdict::Pass);
}
#[test]
fn ac003_pass_many_matches() {
assert_eq!(verdict_from_rag_indexed(50), RagIndexedVerdict::Pass);
}
#[test]
fn ac003_fail_zero_matches() {
assert_eq!(verdict_from_rag_indexed(0), RagIndexedVerdict::Fail);
}
// -------------------------------------------------------------------------
// Section 5: APRCORPUS-004 freshness.
// -------------------------------------------------------------------------
#[test]
fn ac004_pass_recent_commit() {
assert_eq!(verdict_from_corpus_freshness(7), CorpusFreshnessVerdict::Pass);
}
#[test]
fn ac004_pass_at_threshold() {
// The contract says "older than 180 days" — exactly 180 still passes.
assert_eq!(verdict_from_corpus_freshness(180), CorpusFreshnessVerdict::Pass);
}
#[test]
fn ac004_fail_one_day_past() {
assert_eq!(verdict_from_corpus_freshness(181), CorpusFreshnessVerdict::Fail);
}
#[test]
fn ac004_fail_long_stale() {
assert_eq!(verdict_from_corpus_freshness(365), CorpusFreshnessVerdict::Fail);
}
#[test]
fn ac004_pass_zero_days() {
// Just-pushed corpus.
assert_eq!(verdict_from_corpus_freshness(0), CorpusFreshnessVerdict::Pass);
}
// -------------------------------------------------------------------------
// Section 6: APRCORPUS-005 API consistency.
// -------------------------------------------------------------------------
#[test]
fn ac005_pass_no_contradictions() {
assert_eq!(verdict_from_api_consistency(0), ApiConsistencyVerdict::Pass);
}
#[test]
fn ac005_fail_one_contradiction() {
assert_eq!(verdict_from_api_consistency(1), ApiConsistencyVerdict::Fail);
}
#[test]
fn ac005_fail_many_contradictions() {
assert_eq!(verdict_from_api_consistency(20), ApiConsistencyVerdict::Fail);
}
// -------------------------------------------------------------------------
// Section 7: Realistic — full healthy corpus passes all 5.
// -------------------------------------------------------------------------
#[test]
fn realistic_full_healthy_corpus_passes_all_5() {
// Maps to a typical paiml/<lang>-ground-truth-corpus repo state:
// 35 test files, mapping documented, RAG returns 12 matches,
// last commit 14 days ago, no API contradictions.
assert_eq!(verdict_from_corpus_test_files(35), CorpusTestFilesVerdict::Pass);
assert_eq!(verdict_from_rust_mapping(true), RustMappingVerdict::Pass);
assert_eq!(verdict_from_rag_indexed(12), RagIndexedVerdict::Pass);
assert_eq!(verdict_from_corpus_freshness(14), CorpusFreshnessVerdict::Pass);
assert_eq!(verdict_from_api_consistency(0), ApiConsistencyVerdict::Pass);
}
#[test]
fn realistic_pre_fix_all_5_failures() {
// The exact regression class for each gate.
assert_eq!(verdict_from_corpus_test_files(0), CorpusTestFilesVerdict::Fail);
assert_eq!(verdict_from_rust_mapping(false), RustMappingVerdict::Fail);
assert_eq!(verdict_from_rag_indexed(0), RagIndexedVerdict::Fail);
assert_eq!(verdict_from_corpus_freshness(365), CorpusFreshnessVerdict::Fail);
assert_eq!(verdict_from_api_consistency(3), ApiConsistencyVerdict::Fail);
}
// -------------------------------------------------------------------------
// Section 8: Family coverage — verdicts are corpus-identity-agnostic.
// -------------------------------------------------------------------------
#[test]
fn family_coverage_uniform_schema() {
// The 13 apr-corpus-* contracts share this falsification schema.
// Verdict logic doesn't depend on which corpus is being checked.
let corpora = [
"vllm-ground-truth",
"jax-ground-truth",
"hugging-face-ground-truth",
"lean-ground-truth",
"ludwig-ground-truth",
"tgi-ground-truth",
"databricks-ground-truth",
"databricks-scala-ground-truth",
"mixed-python-rust-ground-truth",
"mixed-rust-lean-ground-truth",
"safe-lua-groundtruth",
"tiny-model-ground-truth",
"algorithm-competition-corpus",
];
assert_eq!(corpora.len(), 13);
for c in corpora {
assert_eq!(
verdict_from_corpus_test_files(10),
CorpusTestFilesVerdict::Pass,
"corpus {c} healthy"
);
}
}
}