synapse
A component of the AienOS ecosystem.
Brought by drakestapleton
synapse / crates / synapse-bench / tests / c1_tree_parity.rs
| 1 | //! C1 acceptance (ADR 0003 section 9 step 4): tree and diff parity with `git ls-tree -r` and |
| 2 | //! `git diff-tree`, per entry (mode, type, object id, path) and in order. |
| 3 | //! |
| 4 | //! The CI tests use a synthetic repository plus a Gate 4 shaped population (8 agents x 50 |
| 5 | //! proposals) and hand-written edge cases (`T` status, file and directory replacement). The |
| 6 | //! `#[ignore]`d test repeats the population check on the real Gate 4 fixture |
| 7 | //! (`aien-sovereign-core` at the pinned commit); run it with `--ignored` where the fixture is |
| 8 | //! present (`C1_FIXTURE_REPO`, default `~/workspace/aien-sovereign-core`). |
| 9 | |
| 10 | use synapse_api::TreeTarget; |
| 11 | use synapse_bench::fixture; |
| 12 | use synapse_bench::tree_parity::{ |
| 13 | edge_case_proposals, first_mismatch, synthetic_fixture, temp_dir, ParityPair, |
| 14 | }; |
| 15 | use synapse_bench::workload::generate_write_workload; |
| 16 | |
| 17 | /// Gate 4 fixture commit (aien-sovereign-core, pinned by the Gate 4 qualification). |
| 18 | const GATE4_FIXTURE_REV: &str = "1b6e340d48417dfbcfe2df56d48a0696a3a9b250"; |
| 19 | |
| 20 | fn mismatch_report(what: &str, kind: &str, git: &[String], syn: &[String]) -> Option<String> { |
| 21 | first_mismatch(git, syn).map(|m| { |
| 22 | format!( |
| 23 | "{what}: {kind} parity mismatch at entry {} of {} (git) / {} (synapse)\n git: {:?}\n synapse: {:?}", |
| 24 | m.index, |
| 25 | git.len(), |
| 26 | syn.len(), |
| 27 | m.git, |
| 28 | m.synapse |
| 29 | ) |
| 30 | }) |
| 31 | } |
| 32 | |
| 33 | fn assert_tree_parity(pair: &mut ParityPair, target: TreeTarget, commit: &str, what: &str) { |
| 34 | let (git, syn) = pair.tree_pair(target, commit).unwrap(); |
| 35 | assert!(!git.is_empty(), "{what}: git ls-tree returned nothing"); |
| 36 | if let Some(r) = mismatch_report(what, "tree", &git, &syn) { |
| 37 | panic!("{r}"); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | fn assert_diff_parity(pair: &mut ParityPair, target: TreeTarget, commit: &str, what: &str) { |
| 42 | let (git, syn) = pair.diff_pair(target, commit).unwrap(); |
| 43 | if let Some(r) = mismatch_report(what, "diff", &git, &syn) { |
| 44 | panic!("{r}"); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | /// Populate `agents x ops` Gate 4 proposals and check every changeset's tree and diff. |
| 49 | fn population_parity(clone: &std::path::Path, base: &str, agents: usize, ops: usize) -> usize { |
| 50 | let work = temp_dir("population"); |
| 51 | let mut pair = ParityPair::open(clone, base, &work, 0xC1, agents).unwrap(); |
| 52 | let base_target = TreeTarget::BaseGitCommit(pair.base_oid); |
| 53 | assert_tree_parity(&mut pair, base_target, base, "base tree"); |
| 54 | let listed = fixture::list_base_tree(clone, base).unwrap(); |
| 55 | let full = fixture::load_full_base(clone, &listed).unwrap(); |
| 56 | let files = fixture::editable_from_full_base(&listed, &full); |
| 57 | let workload = generate_write_workload(0xC1, 10_000, agents, ops, &files); |
| 58 | let mut checked = 0; |
| 59 | for p in workload.iter().flatten() { |
| 60 | let (cs, commit) = pair.apply(p).unwrap(); |
| 61 | let what = format!("agent {} proposal {}", p.agent, p.index); |
| 62 | assert_tree_parity(&mut pair, TreeTarget::Changeset(cs), &commit, &what); |
| 63 | assert_diff_parity(&mut pair, TreeTarget::Changeset(cs), &commit, &what); |
| 64 | checked += 1; |
| 65 | } |
| 66 | let _ = std::fs::remove_dir_all(&work); |
| 67 | checked |
| 68 | } |
| 69 | |
| 70 | #[test] |
| 71 | fn c1_tree_parity_with_git_ls_tree_r_on_gate4_shaped_population() { |
| 72 | let root = temp_dir("synthetic-population"); |
| 73 | let base = synthetic_fixture(&root, 48, 24).unwrap(); |
| 74 | let checked = population_parity(&root.join("fixture"), &base, 8, 50); |
| 75 | assert_eq!(checked, 400); |
| 76 | let _ = std::fs::remove_dir_all(&root); |
| 77 | } |
| 78 | |
| 79 | #[test] |
| 80 | fn c1_tree_and_diff_parity_with_git_on_edge_cases() { |
| 81 | let root = temp_dir("edge-cases"); |
| 82 | let base = synthetic_fixture(&root, 8, 12).unwrap(); |
| 83 | let clone = root.join("fixture"); |
| 84 | let work = root.join("work"); |
| 85 | let mut pair = ParityPair::open(&clone, &base, &work, 0xC1E, 2).unwrap(); |
| 86 | // Every case is checked before failing, so one run shows each mismatch. |
| 87 | let mut failures = Vec::new(); |
| 88 | for p in edge_case_proposals() { |
| 89 | let (cs, commit) = pair.apply(&p).unwrap(); |
| 90 | let target = TreeTarget::Changeset(cs); |
| 91 | let (git, syn) = pair.tree_pair(target, &commit).unwrap(); |
| 92 | failures.extend(mismatch_report(&p.summary, "tree", &git, &syn)); |
| 93 | let (git, syn) = pair.diff_pair(target, &commit).unwrap(); |
| 94 | failures.extend(mismatch_report(&p.summary, "diff", &git, &syn)); |
| 95 | } |
| 96 | assert!(failures.is_empty(), "{}", failures.join("\n")); |
| 97 | let _ = std::fs::remove_dir_all(&root); |
| 98 | } |
| 99 | |
| 100 | #[test] |
| 101 | #[ignore = "needs the Gate 4 fixture repository (run with --ignored where it is present)"] |
| 102 | fn c1_tree_and_diff_parity_with_git_on_gate4_fixture() { |
| 103 | let src = std::env::var("C1_FIXTURE_REPO").unwrap_or_else(|_| { |
| 104 | format!( |
| 105 | "{}/workspace/aien-sovereign-core", |
| 106 | std::env::var("HOME").unwrap_or_default() |
| 107 | ) |
| 108 | }); |
| 109 | let root = temp_dir("gate4-fixture"); |
| 110 | let clone = root.join("clone"); |
| 111 | fixture::clone_no_checkout(std::path::Path::new(&src), &clone).unwrap(); |
| 112 | let checked = population_parity(&clone, GATE4_FIXTURE_REV, 8, 50); |
| 113 | assert_eq!(checked, 400); |
| 114 | println!("c1 gate4 fixture parity: base tree plus {checked} changesets, tree and diff, per entry: all equal"); |
| 115 | let _ = std::fs::remove_dir_all(&root); |
| 116 | } |