aprender-core 0.34.0

Next-generation machine learning library in pure Rust
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// `cgp-monorepo-build-v1` algorithm-level PARTIAL discharge for the 7
// monorepo build-verification falsifiers (no duplicates, version
// consistency, ≥60 members, no [patch.crates-io], all dirs have
// Cargo.toml, aprender-* naming, flat layout).
//
// Contract: `contracts/cgp-monorepo-build-v1.yaml`.
// Refs: Potvin & Levenberg (CACM 2016), Rastogi et al. (ICSME 2023),
// Burn/Nushell flat-layout monorepos.

use std::collections::HashSet;

/// Minimum workspace member count per FALSIFY-BUILD-003.
pub const AC_MONOREPO_MIN_MEMBERS: usize = 60;

/// Workspace version per metadata (0.29.0 at contract creation,
/// pinned for the FALSIFY-BUILD-002 gate).
pub const AC_MONOREPO_WORKSPACE_VERSION: &str = "0.29.0";

/// Crate names exempt from the aprender-* naming convention.
/// `aprender` is the root facade (cargo install aprender → apr binary);
/// `apr-cli` is the internal CLI logic crate per the contract description.
pub const AC_MONOREPO_NAME_EXEMPTIONS: [&str; 2] = ["aprender", "apr-cli"];

/// Legacy crate names that MUST NOT appear as workspace [package] names.
pub const AC_MONOREPO_FORBIDDEN_NAMES: [&str; 4] =
    ["trueno", "realizar", "entrenar", "batuta"];

// =============================================================================
// FALSIFY-BUILD-001 — no duplicate workspace members
// =============================================================================

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NoDuplicatesVerdict {
    /// All [package] names are unique.
    Pass,
    /// At least two members share a name — merge conflict.
    Fail,
}

#[must_use]
pub fn verdict_from_no_duplicates(member_names: &[&str]) -> NoDuplicatesVerdict {
    if member_names.is_empty() {
        return NoDuplicatesVerdict::Fail;
    }
    let mut seen: HashSet<&&str> = HashSet::new();
    for n in member_names {
        if !seen.insert(n) {
            return NoDuplicatesVerdict::Fail;
        }
    }
    NoDuplicatesVerdict::Pass
}

// =============================================================================
// FALSIFY-BUILD-002 — workspace version consistency
// =============================================================================

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum VersionConsistencyVerdict {
    /// All members using `version.workspace = true` resolve to 0.29.0.
    Pass,
    /// Workspace version mismatch — root Cargo.toml drift.
    Fail,
}

#[must_use]
pub fn verdict_from_version_consistency(resolved_workspace_version: &str) -> VersionConsistencyVerdict {
    if resolved_workspace_version == AC_MONOREPO_WORKSPACE_VERSION {
        VersionConsistencyVerdict::Pass
    } else {
        VersionConsistencyVerdict::Fail
    }
}

// =============================================================================
// FALSIFY-BUILD-003 — workspace member count ≥ 60
// =============================================================================

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MinMemberCountVerdict {
    /// `cargo metadata --workspace` reports ≥ 60 members.
    Pass,
    /// Below threshold — crates accidentally excluded.
    Fail,
}

#[must_use]
pub fn verdict_from_min_member_count(actual_count: usize) -> MinMemberCountVerdict {
    if actual_count >= AC_MONOREPO_MIN_MEMBERS {
        MinMemberCountVerdict::Pass
    } else {
        MinMemberCountVerdict::Fail
    }
}

// =============================================================================
// FALSIFY-BUILD-004 — no [patch.crates-io] in root Cargo.toml
// =============================================================================

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NoPatchVerdict {
    /// Root Cargo.toml does not contain `[patch.crates-io]` section.
    Pass,
    /// Patch section leaked into root.
    Fail,
}

#[must_use]
pub fn verdict_from_no_patch(root_cargo_toml: &str) -> NoPatchVerdict {
    if root_cargo_toml.contains("[patch.crates-io]") {
        NoPatchVerdict::Fail
    } else {
        NoPatchVerdict::Pass
    }
}

// =============================================================================
// FALSIFY-BUILD-005 — all workspace dirs have Cargo.toml
// =============================================================================

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AllHaveCargoTomlVerdict {
    /// Every workspace member dir contains a Cargo.toml file.
    Pass,
    /// At least one is missing — broken subtree merge.
    Fail,
}

/// `(dir_name, has_cargo_toml)` per workspace member directory.
#[must_use]
pub fn verdict_from_all_have_cargo_toml(member_dirs: &[(&str, bool)]) -> AllHaveCargoTomlVerdict {
    if member_dirs.is_empty() {
        return AllHaveCargoTomlVerdict::Fail;
    }
    for (_dir, has_cargo) in member_dirs {
        if !*has_cargo {
            return AllHaveCargoTomlVerdict::Fail;
        }
    }
    AllHaveCargoTomlVerdict::Pass
}

// =============================================================================
// FALSIFY-BUILD-006 — all crate names use aprender-* prefix (or apr-cli)
// =============================================================================

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AprPrefixVerdict {
    /// Every package name is "apr-cli" OR starts with "aprender-",
    /// AND no member uses a forbidden legacy name.
    Pass,
    /// Some package violates the naming convention.
    Fail,
}

#[must_use]
pub fn verdict_from_apr_prefix(member_names: &[&str]) -> AprPrefixVerdict {
    if member_names.is_empty() {
        return AprPrefixVerdict::Fail;
    }
    for name in member_names {
        if AC_MONOREPO_FORBIDDEN_NAMES.contains(name) {
            return AprPrefixVerdict::Fail;
        }
        if AC_MONOREPO_NAME_EXEMPTIONS.contains(name) {
            continue;
        }
        if !name.starts_with("aprender-") {
            return AprPrefixVerdict::Fail;
        }
    }
    AprPrefixVerdict::Pass
}

// =============================================================================
// FALSIFY-BUILD-007 — flat layout (no nested crates)
// =============================================================================

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FlatLayoutVerdict {
    /// Every Cargo.toml path is `crates/<name>/Cargo.toml` (depth 2 from
    /// crates/, not deeper).
    Pass,
    /// Nested Cargo.toml found — flat-layout invariant violated.
    Fail,
}

#[must_use]
pub fn verdict_from_flat_layout(cargo_toml_paths: &[&str]) -> FlatLayoutVerdict {
    if cargo_toml_paths.is_empty() {
        return FlatLayoutVerdict::Fail;
    }
    for path in cargo_toml_paths {
        if !path.starts_with("crates/") {
            return FlatLayoutVerdict::Fail;
        }
        let relative = &path["crates/".len()..];
        // Expected: `<name>/Cargo.toml` (one slash). Anything deeper = nested.
        let slashes = relative.matches('/').count();
        if slashes != 1 {
            return FlatLayoutVerdict::Fail;
        }
        if !path.ends_with("/Cargo.toml") {
            return FlatLayoutVerdict::Fail;
        }
    }
    FlatLayoutVerdict::Pass
}

#[cfg(test)]
mod tests {
    use super::*;

    // -------------------------------------------------------------------------
    // Section 1: Provenance pins.
    // -------------------------------------------------------------------------
    #[test]
    fn provenance_min_members_60() {
        assert_eq!(AC_MONOREPO_MIN_MEMBERS, 60);
    }

    #[test]
    fn provenance_workspace_version_0_29() {
        assert_eq!(AC_MONOREPO_WORKSPACE_VERSION, "0.29.0");
    }

    #[test]
    fn provenance_naming_exemptions_contains_apr_cli() {
        assert!(AC_MONOREPO_NAME_EXEMPTIONS.contains(&"apr-cli"));
    }

    #[test]
    fn provenance_forbidden_names_count_4() {
        assert_eq!(AC_MONOREPO_FORBIDDEN_NAMES.len(), 4);
    }

    // -------------------------------------------------------------------------
    // Section 2: BUILD-001 no duplicates.
    // -------------------------------------------------------------------------
    #[test]
    fn fb001_pass_unique_members() {
        let m = ["aprender", "aprender-core", "apr-cli"];
        assert_eq!(verdict_from_no_duplicates(&m), NoDuplicatesVerdict::Pass);
    }

    #[test]
    fn fb001_fail_duplicate() {
        let m = ["aprender-core", "aprender-train", "aprender-core"];
        assert_eq!(verdict_from_no_duplicates(&m), NoDuplicatesVerdict::Fail);
    }

    #[test]
    fn fb001_fail_empty() {
        assert_eq!(
            verdict_from_no_duplicates(&[]),
            NoDuplicatesVerdict::Fail
        );
    }

    // -------------------------------------------------------------------------
    // Section 3: BUILD-002 version consistency.
    // -------------------------------------------------------------------------
    #[test]
    fn fb002_pass_canonical_version() {
        assert_eq!(
            verdict_from_version_consistency("0.29.0"),
            VersionConsistencyVerdict::Pass
        );
    }

    #[test]
    fn fb002_fail_old_version() {
        assert_eq!(
            verdict_from_version_consistency("0.28.0"),
            VersionConsistencyVerdict::Fail
        );
    }

    #[test]
    fn fb002_fail_future_version() {
        assert_eq!(
            verdict_from_version_consistency("0.30.0"),
            VersionConsistencyVerdict::Fail
        );
    }

    // -------------------------------------------------------------------------
    // Section 4: BUILD-003 minimum member count.
    // -------------------------------------------------------------------------
    #[test]
    fn fb003_pass_at_threshold() {
        assert_eq!(verdict_from_min_member_count(60), MinMemberCountVerdict::Pass);
    }

    #[test]
    fn fb003_pass_above_threshold() {
        assert_eq!(verdict_from_min_member_count(70), MinMemberCountVerdict::Pass);
    }

    #[test]
    fn fb003_fail_below_threshold() {
        assert_eq!(verdict_from_min_member_count(59), MinMemberCountVerdict::Fail);
    }

    // -------------------------------------------------------------------------
    // Section 5: BUILD-004 no [patch.crates-io].
    // -------------------------------------------------------------------------
    #[test]
    fn fb004_pass_clean_root() {
        let toml = "[workspace]\nmembers = [\"crates/*\"]";
        assert_eq!(verdict_from_no_patch(toml), NoPatchVerdict::Pass);
    }

    #[test]
    fn fb004_fail_patch_present() {
        let toml = "[workspace]\nmembers = [\"crates/*\"]\n[patch.crates-io]\nfoo = { path = \"local\" }";
        assert_eq!(verdict_from_no_patch(toml), NoPatchVerdict::Fail);
    }

    // -------------------------------------------------------------------------
    // Section 6: BUILD-005 all dirs have Cargo.toml.
    // -------------------------------------------------------------------------
    #[test]
    fn fb005_pass_all_present() {
        let dirs = [("aprender-core", true), ("apr-cli", true)];
        assert_eq!(
            verdict_from_all_have_cargo_toml(&dirs),
            AllHaveCargoTomlVerdict::Pass
        );
    }

    #[test]
    fn fb005_fail_missing_cargo_toml() {
        let dirs = [("aprender-core", true), ("aprender-train", false)];
        assert_eq!(
            verdict_from_all_have_cargo_toml(&dirs),
            AllHaveCargoTomlVerdict::Fail
        );
    }

    #[test]
    fn fb005_fail_empty() {
        assert_eq!(
            verdict_from_all_have_cargo_toml(&[]),
            AllHaveCargoTomlVerdict::Fail
        );
    }

    // -------------------------------------------------------------------------
    // Section 7: BUILD-006 aprender-* naming.
    // -------------------------------------------------------------------------
    #[test]
    fn fb006_pass_canonical_names() {
        let m = ["aprender", "aprender-core", "aprender-train", "apr-cli"];
        assert_eq!(verdict_from_apr_prefix(&m), AprPrefixVerdict::Pass);
    }

    #[test]
    fn fb006_fail_legacy_name() {
        let m = ["aprender-core", "trueno"];
        assert_eq!(verdict_from_apr_prefix(&m), AprPrefixVerdict::Fail);
    }

    #[test]
    fn fb006_fail_non_aprender_prefix() {
        let m = ["aprender-core", "random-crate"];
        assert_eq!(verdict_from_apr_prefix(&m), AprPrefixVerdict::Fail);
    }

    #[test]
    fn fb006_fail_each_legacy_individually() {
        for legacy in AC_MONOREPO_FORBIDDEN_NAMES {
            let m = vec!["aprender-core", legacy];
            assert_eq!(
                verdict_from_apr_prefix(&m),
                AprPrefixVerdict::Fail,
                "legacy name {legacy} must Fail"
            );
        }
    }

    // -------------------------------------------------------------------------
    // Section 8: BUILD-007 flat layout.
    // -------------------------------------------------------------------------
    #[test]
    fn fb007_pass_all_flat() {
        let p = [
            "crates/aprender-core/Cargo.toml",
            "crates/aprender-train/Cargo.toml",
        ];
        assert_eq!(verdict_from_flat_layout(&p), FlatLayoutVerdict::Pass);
    }

    #[test]
    fn fb007_fail_nested_crate() {
        let p = [
            "crates/aprender-core/Cargo.toml",
            "crates/aprender-core/sub-crate/Cargo.toml",
        ];
        assert_eq!(verdict_from_flat_layout(&p), FlatLayoutVerdict::Fail);
    }

    #[test]
    fn fb007_fail_outside_crates_dir() {
        let p = ["src/main.rs/Cargo.toml"];
        assert_eq!(verdict_from_flat_layout(&p), FlatLayoutVerdict::Fail);
    }

    #[test]
    fn fb007_fail_empty() {
        assert_eq!(verdict_from_flat_layout(&[]), FlatLayoutVerdict::Fail);
    }

    // -------------------------------------------------------------------------
    // Section 9: Realistic — full healthy monorepo passes all 7.
    // -------------------------------------------------------------------------
    #[test]
    fn realistic_healthy_monorepo_passes_all_7() {
        // 001
        let names = ["aprender", "aprender-core", "aprender-train", "apr-cli"];
        assert_eq!(verdict_from_no_duplicates(&names), NoDuplicatesVerdict::Pass);
        // 002
        assert_eq!(
            verdict_from_version_consistency("0.29.0"),
            VersionConsistencyVerdict::Pass
        );
        // 003
        assert_eq!(verdict_from_min_member_count(70), MinMemberCountVerdict::Pass);
        // 004
        let toml = "[workspace]\nmembers = [\"crates/*\"]\nresolver = \"2\"";
        assert_eq!(verdict_from_no_patch(toml), NoPatchVerdict::Pass);
        // 005
        let dirs = [("aprender-core", true), ("apr-cli", true)];
        assert_eq!(
            verdict_from_all_have_cargo_toml(&dirs),
            AllHaveCargoTomlVerdict::Pass
        );
        // 006
        assert_eq!(verdict_from_apr_prefix(&names), AprPrefixVerdict::Pass);
        // 007
        let paths = [
            "crates/aprender/Cargo.toml",
            "crates/aprender-core/Cargo.toml",
        ];
        assert_eq!(verdict_from_flat_layout(&paths), FlatLayoutVerdict::Pass);
    }

    #[test]
    fn realistic_pre_fix_all_7_failures() {
        // 001: subtree merge created two `trueno` entries (one renamed, one not).
        assert_eq!(
            verdict_from_no_duplicates(&["aprender-core", "aprender-core"]),
            NoDuplicatesVerdict::Fail
        );
        // 002: stale workspace version.
        assert_eq!(
            verdict_from_version_consistency("0.20.0"),
            VersionConsistencyVerdict::Fail
        );
        // 003: workspace member missing — only 30.
        assert_eq!(verdict_from_min_member_count(30), MinMemberCountVerdict::Fail);
        // 004: dev override leaked into root Cargo.toml.
        let bad_toml = "[patch.crates-io]\ntrueno = { path = \"../trueno\" }";
        assert_eq!(verdict_from_no_patch(bad_toml), NoPatchVerdict::Fail);
        // 005: subtree merge dropped Cargo.toml.
        assert_eq!(
            verdict_from_all_have_cargo_toml(&[("aprender-core", false)]),
            AllHaveCargoTomlVerdict::Fail
        );
        // 006: legacy name `realizar` still in workspace.
        assert_eq!(
            verdict_from_apr_prefix(&["aprender-core", "realizar"]),
            AprPrefixVerdict::Fail
        );
        // 007: nested crate at crates/foo/bar/Cargo.toml.
        assert_eq!(
            verdict_from_flat_layout(&["crates/foo/bar/Cargo.toml"]),
            FlatLayoutVerdict::Fail
        );
    }
}