diff --git a/native/aetherlight/PROVENANCE.md b/native/aetherlight/PROVENANCE.md index be2799b84..5aba5ac0f 100644 --- a/native/aetherlight/PROVENANCE.md +++ b/native/aetherlight/PROVENANCE.md @@ -522,3 +522,25 @@ their place's grain, every tenth person from the 35th retired, and the clock moved one period so every routine is past due. The fixed-count pins on `dependencies` and `redress` are gone; `read_work_is_equal_at_100_and_1600` now asserts the population at scale. + +Soul pass 1 (fix batch). The fold's moved-targets arm reads the dependencies +`DependsOn` index through `Docs::indexed_unchanged`, which names the committed +entries of keys the open log did not change and does not walk the log; a changed +dependencies key is already re-judged whole, so a bulk commit (genesis, every +replayed commit at open) folds in rows proportional to what it changed +(`folding_a_bulk_commit_reads_rows_in_proportion_to_the_change`). The clean-log +check on a view read is an `assert!`, not a `debug_assert!`: six flag tests, held +in every build. `Release` now clears the pressure its dependency raised through +`set_pressure`; before, a pressed dependency's pressure row outlived it and the +export failed to reopen (pre-existing at `609b3ba5e`). A seeded differential test +(`views_equal_their_rebuild_and_the_scans_over_random_commands`) holds the views +to their rebuild and the motion to the scans over random bind, release, route, +holding, relocate, retire, tick, speech and refused commands, reopening every 50. + +Not defended by the gate, on purpose: a tick's cost is linear in the number of +failing dependencies, because each failing dependency presses a pressure row each +tick. The gate world pins failing at two, so growth in failing is not seen; that +cost is inherent to pressing and is not a read of the world. `open` of a large +world is superlinear in rows from readers outside this cut (the resolver's reads +of the whole world at genesis, owner `aetherlight-resolver-reads-world`); +`open_cost` reports it. diff --git a/native/aetherlight/src/golden_tests.rs b/native/aetherlight/src/golden_tests.rs index 242ca2a6e..4a801b748 100644 --- a/native/aetherlight/src/golden_tests.rs +++ b/native/aetherlight/src/golden_tests.rs @@ -1629,3 +1629,159 @@ fn folding_a_bulk_commit_reads_rows_in_proportion_to_the_change() { assert!(rows[1] > rows[0], "{rows:?}: a bigger commit read no more"); assert!(rows[1] < rows[0] * 6, "the fold read {} rows at 400 persons and {} at 1600", rows[0], rows[1]); } + +/// Every kind of command that moves what the views join, drawn at random from a +/// seeded stream over the clockwork world (bind and release on a subject, a +/// route and a resource; routes closed and opened; holdings emptied and filled; +/// subjects relocated and retired; ticks and speech; refused commands), with the +/// world reopened from its export every 50 commits. After every commit, kept or +/// refused, the views are what a scan of the documents builds and the motion +/// they derive is the motion the scans derive, over short and long spans; each +/// reopened world holds the live views. Seeds are fixed, so a failure replays. +#[test] +fn views_equal_their_rebuild_and_the_scans_over_random_commands() { + use crate::patch::{DependencyRef, EvidenceRef, Quantity}; + use crate::tests::TITHE_RECEIPT; + use crate::DependencyTarget; + use std::collections::BTreeMap; + + let mut kept: BTreeMap<&'static str, usize> = BTreeMap::new(); + for seed in [1u64, 2, 3] { + let (mut kernel, people, speak_entry) = clockwork_world(40); + let routes: Vec<_> = kernel.state.edges.keys().copied().collect(); + let grains: Vec<_> = (0..4) + .map(|place| { + *kernel + .state + .entities + .iter() + .find(|(_, record)| record.label == format!("Grain {place}")) + .expect("a declared resource") + .0 + }) + .collect(); + let mut state = seed.wrapping_mul(0x9E37_79B9_7F4A_7C15) | 1; + let mut next = move |bound: usize| { + state ^= state << 13; + state ^= state >> 7; + state ^= state << 17; + (state % bound as u64) as usize + }; + for step in 0..150 { + let person = people[next(people.len())]; + let route = routes[next(routes.len())]; + let grain = grains[next(grains.len())]; + let target = match next(3) { + 0 => DependencyRef::Subject(Ref::Existing(people[next(people.len())])), + 1 => DependencyRef::Route(Ref::Existing(route)), + _ => DependencyRef::Resource(Ref::Existing(grain)), + }; + let ops = |op| operations(vec![op]); + let (name, body) = match next(16) { + 0 | 1 => ("bind", ops(ComponentOp::Bind { subject: Ref::Existing(person), target })), + 2 | 3 => { + // Release something that is bound, so releases succeed. + let bound: Vec<_> = kernel + .state + .dependencies + .iter() + .flat_map(|(subject, held)| held.iter().map(|target| (*subject, *target))) + .collect(); + if bound.is_empty() { + ("tick", tick_body(1)) + } else { + let (subject, held) = bound[next(bound.len())]; + let target = match held { + DependencyTarget::Subject(other) => DependencyRef::Subject(Ref::Existing(other)), + DependencyTarget::Route(edge) => DependencyRef::Route(Ref::Existing(edge)), + DependencyTarget::Resource(entity) => DependencyRef::Resource(Ref::Existing(entity)), + }; + ("release", ops(ComponentOp::Release { subject: Ref::Existing(subject), target })) + } + } + 4 => ("close", ops(ComponentOp::CloseRoute { route: Ref::Existing(route) })), + 5 => ("open", ops(ComponentOp::OpenRoute { route: Ref::Existing(route) })), + 6 => ( + "consume", + ops(ComponentOp::Consume { + holder: Ref::Existing(person), + resource: Ref::Existing(grain), + qty: Quantity(1), + }), + ), + 7 => ( + "admit", + CommandBody::AdmitPatch { + patch: WorldPatch { + declarations: Vec::new(), + operations: vec![ComponentOp::Admit { + holder: Ref::Existing(person), + resource: Ref::Existing(grain), + qty: Quantity(1), + evidence: EvidenceRef::new(TITHE_RECEIPT), + }], + evidence: vec![EvidenceRef::new(TITHE_RECEIPT)], + }, + }, + ), + 8 => { + // Along a route that touches where the subject stands. + let here = kernel.state.positions.get(&person).map(|position| position.place); + let via = kernel + .state + .edges + .iter() + .find(|(_, edge)| { + let (from, to) = edge.endpoints(); + here.is_some_and(|place| place == from || place == to) + }) + .map(|(edge, _)| *edge) + .unwrap_or(route); + ("relocate", ops(ComponentOp::Relocate { subject: Ref::Existing(person), via: Ref::Existing(via) })) + } + 9 if next(5) == 0 => ("retire", ops(ComponentOp::Retire { subject: Ref::Existing(person) })), + 13 => ( + "speech", + CommandBody::Act { + actor: person, + invocation: speak(speak_entry, &format!("Speech {step}.")), + }, + ), + 14 => ( + "refused", + ops(ComponentOp::Bind { + subject: Ref::Existing(person), + target: DependencyRef::Subject(Ref::Existing(person)), + }), + ), + _ => ("tick", tick_body(1 + next(25) as u32)), + }; + let envelope = clockwork_envelope(&kernel, body); + if kernel.submit(envelope, &AuthenticatedCaller::verified_play()).is_ok() { + *kept.entry(name).or_default() += 1; + } + let label = format!("seed {seed}, commit {step} ({name})"); + assert!( + kernel.state.views.same_as(&crate::views::Views::rebuild(&kernel.state)), + "{label}: the views left their documents" + ); + for minutes in [1, 30, 5_000] { + let to = FictionalMinutes(kernel.state.header.now.0 + minutes); + assert_eq!( + crate::clock::derive_motion(&kernel.state, to), + crate::clock::derive_motion_by_scan(&kernel.state, to), + "{label}: motion over {minutes} minutes" + ); + } + if step % 50 == 49 { + let reopened = WorldKernel::open(&kernel.export().unwrap()) + .unwrap_or_else(|error| panic!("{label}: the world does not reopen: {error:?}")); + assert_eq!(reopened.state, kernel.state, "{label}: reopened state"); + assert!(reopened.state.views.same_as(&kernel.state.views), "{label}: reopened views"); + } + } + } + for name in ["bind", "release", "close", "open", "consume", "admit", "relocate", "retire", "speech", "tick"] { + assert!(kept.get(name).copied().unwrap_or(0) > 0, "no {name} ever landed: {kept:?}"); + } +}