diff --git a/native/aetherlight/PROVENANCE.md b/native/aetherlight/PROVENANCE.md index 5aba5ac0f..d0cbdd073 100644 --- a/native/aetherlight/PROVENANCE.md +++ b/native/aetherlight/PROVENANCE.md @@ -544,3 +544,73 @@ 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. + +## Cut AL3d-1: the patch lane reads what the patch touches + +Authority: cut `aetherlight-resolver-reads-world` r1 of campaign +`aetheria-release`; rulings `cultnet-scale-is-not-negotiable`, +`aetherlight-narrative-state-owner`, `terminus-clockwork-world`, +`dialogue-is-world-action`. Base `609b3ba5e`. No digested byte moves: +`fixtures/golden.digests` is identical at every commit of the cut. + +`resolve_patch` used to seed fifteen candidate maps by copying every partition, +collect every catalog kind and every subject's import key into sets, and re-judge +every holder for overlap after each widening operation. Its reads grew with the +world: 936, 3696 and 14736 rows for one `DischargeCommitment` at 100, 400 and 1600 +persons, and 14,000 or more for every other shape. It now reads what the patch +touches. + +`patch/candidate.rs` owns the candidate graph of one resolution. Each candidate map +is an `Overlay`: the committed partition read through a base function (a point get; +a `Key::Draft` key has none), and the patch's writes in a local map. `Candidate` +holds the fifteen overlays and the named reads, each bounded by what the patch +wrote plus what it returns: `occupants_under` (`under_place` over `ChildOf` and +`StandsAt`, corrected by the written positions), `offices_of` and `offices_held_by` +(the committed row or `ByIncumbent`, corrected by the written offices), `kind_taken` +(`ByKind` on the catalog) and `import_key_held` (`ByImportKey` on the subjects), +`custody` (the sums over the written holders only: every other holder is on both +sides of the equation and cancels). `authority`, `selection` and `retired` are +private to the module: a write goes through `set_grants`, `set_office` or `retire`, +which re-judge the holders whose effective authority it can change (the holder and +an institution's incumbents; an office's old and new incumbent) and keep +`overlapping`, the set of holders with two grants of one kind over overlapping +ground. A widening operation is `OverlappingJurisdiction` exactly when the set is +non-empty. That is what `graph_overlaps` answered, because the committed world is +disjoint (every commit checks it and opening a world verifies it), so only a holder +the patch touched can enter the set. The mismatch list is unchanged, including the +later widening operations after a first overlap and a revoke that clears it +(`overlap_mismatches_are_unchanged`). The candidate lives for one `resolve_patch` +call and is never written back; `admit_resolved` stays the only writer of the +ontology partitions. + +`apply_operations` derives its two checks from the transaction's log. Conservation +sums the holders `holdings` logged as changed, each from its value when the +operations began (so a holder already written by the declarations above it is +measured from there). Disjointness judges only the holders whose grants, offices or +institution the transaction touched, through `holder_overlaps`, which +`overlapping_holder` also calls for the open-time whole-world check +(`verify_state_shape`, whole-world on purpose). `resource_total` survives only as +the test reference `resource_total_by_scan`. + +Two partitions gained read indexes, `subjects` by `ByImportKey` and +`affordance_catalog` by `ByKind`, so `held_import_keys` and the per-patch kind set +are gone and `import_key_fault` asks a predicate. + +The seeding block, `graph_overlaps`, the old effective-authority walk and +`key_covers_place` live on only as `cfg(test)` references (`Materialized`, whose +`seeded` is the old block verbatim). `candidate_reads_equal_materialized_scans` +walks a civic world through the shapes that move each read (a subject leaving and +entering a subtree, a declared place and subject, a retirement, a vacated and +refilled office, an overlapping grant and its revocation, the retirement of the +lending institution, a declared institution, custody written both ways) and holds +every named read and the overlap set to the scan after every step; +`an_untouched_overlay_reads_what_the_seeding_copied` holds every base function to +the copy in each populated world. The gate is +`golden_tests::patch_read_work_is_equal_at_100_and_1600`: ten patch shapes through +`AdmitPatch` read the same rows at 100, 400 and 1600 persons (at most 21 each), +and `apply_checks_read_the_log` pins the commit lane's two checks. The +`DischargeCommitment` case of `operation_reads_are_equal_at_100_and_1600`, which +called `apply_operation` directly, moved into the gate. + +Reads that still scan: `snapshot` (follow-up `aetherlight-snapshot-history-fold`) +and `verify_state_shape` at open, by design. diff --git a/native/aetherlight/src/golden_tests.rs b/native/aetherlight/src/golden_tests.rs index 2d91d55a5..fa929271f 100644 --- a/native/aetherlight/src/golden_tests.rs +++ b/native/aetherlight/src/golden_tests.rs @@ -1041,16 +1041,224 @@ fn read_work_is_equal_at_100_and_1600() { assert_eq!(worked[0], worked[1], "read work grew with the world"); } -/// The reads a clockwork submit does not reach, inside one open transaction -/// each: who stands under a two-level subtree, discharging a commitment while -/// the world holds N pressure rows from other sources, one incumbent's -/// delegated authority, and a path of three routes. (An `AdmitPatch` resolves -/// against the whole world until follow-up `aetherlight-resolver-reads-world`, -/// so no submit shape reaches these.) +/// What a patch reads is what it touches, not what the world holds: ten patch +/// shapes, each submitted through `AdmitPatch` (resolution against the +/// candidate graph, the reducer, and the commit's own checks), read the same +/// number of rows at 100, 400 and 1600 persons (some read none: point reads are +/// not rows). The count is taken where reads +/// are owned (`Docs`), so a resolver arm that copies a partition, collects keys +/// from every subject, sums a resource over every holder, or judges every holder +/// for overlap cannot hide from it. The shapes run in order on one world, so +/// each leaves the world the next one reads. +#[test] +fn patch_read_work_is_equal_at_100_and_1600() { + use crate::patch::{ + AuthorityGrantRef, AuthorityTargetRef, AuthoredSource, DependencyRef, FactDeclaration, + FactStandingRef, PressureMagnitude, PressureSourceRef, Quantity, SubjectDeclaration, + }; + use crate::tests::{LEVY_KIND, WARDEN_OFFICE, authority_kind, office}; + use crate::{Confidence, ImportKey, SubjectKind}; + use std::collections::BTreeSet; + + let mut shapes: Vec<&'static str> = Vec::new(); + let mut worked: Vec> = Vec::new(); + for persons in [100usize, 400, 1600] { + let (mut kernel, people, _) = clockwork_world(persons); + let label = |kernel: &WorldKernel, label: &str| { + *kernel + .state + .entities + .iter() + .find(|(_, record)| record.label == label) + .unwrap_or_else(|| panic!("{label} is declared")) + .0 + }; + let (place_zero, grain, fact_one) = ( + label(&kernel, "Place 0"), + label(&kernel, "Grain 0"), + label(&kernel, "Fact 1"), + ); + let route = *kernel + .state + .edges + .iter() + .find(|(_, record)| record.label() == "Route 0") + .expect("the first route is declared") + .0; + let institution = subject_by_label(&kernel.state, "Institution 0"); + + // The tick presses the two obligations; then every other person is + // pressed by one source, so the pressure partition holds a row for each. + kernel + .submit(clockwork_envelope(&kernel, tick_body(1)), &AuthenticatedCaller::verified_play()) + .expect("the tick presses the obligations"); + let pressing: Vec = people[1..] + .iter() + .map(|person| ComponentOp::AdvancePressure { + source: PressureSourceRef::Subject(Ref::Existing(people[0])), + target: Ref::Existing(*person), + by: PressureMagnitude(1), + }) + .collect(); + kernel + .submit( + clockwork_envelope(&kernel, operations(pressing)), + &AuthenticatedCaller::verified_play(), + ) + .expect("the pressure rows land"); + assert!(kernel.state.pressures.len() >= persons, "the pressure partition is at scale"); + let obligation = kernel.state.commitments[&people[0]] + .iter() + .find(|(_, commitment)| commitment.kind == CommitmentKind::Obligation) + .map(|(key, _)| *key) + .expect("the first person owes an obligation"); + + let patch = |declarations: Vec, operations: Vec| { + CommandBody::AdmitPatch { + patch: WorldPatch { declarations, operations, evidence: Vec::new() }, + } + }; + let bodies: Vec<(&'static str, CommandBody)> = vec![ + ( + "discharge a past-due obligation", + patch( + Vec::new(), + vec![ComponentOp::DischargeCommitment { + subject: Ref::Existing(people[0]), + key: obligation, + }], + ), + ), + ( + "relocate along a route", + patch( + Vec::new(), + vec![ComponentOp::Relocate { subject: Ref::Existing(people[5]), via: Ref::Existing(route) }], + ), + ), + ( + "declare a subject with an import key", + patch( + vec![Declaration::Subject(SubjectDeclaration { + handle: DraftHandle::new("newcomer"), + label: "The Newcomer".into(), + kind: SubjectKind::Person, + affordances: BTreeSet::new(), + position: Some(Ref::Existing(place_zero)), + import_key: Some(ImportKey::new("aetherlight.gate", "newcomer")), + })], + Vec::new(), + ), + ), + ( + "transfer grain", + patch( + Vec::new(), + vec![ComponentOp::Transfer { + from: Ref::Existing(people[0]), + to: Ref::Existing(people[1]), + resource: Ref::Existing(grain), + qty: Quantity(1), + }], + ), + ), + ( + "witness a fact declared in the same patch", + patch( + vec![Declaration::Fact(FactDeclaration { + handle: DraftHandle::new("omen"), + label: "The Omen".into(), + statement: Statement::new("The lamp gutters.").unwrap(), + standing: FactStandingRef::Ruled, + })], + vec![ComponentOp::Witness { + fact: Ref::Draft(DraftHandle::new("omen")), + place: Ref::Existing(place_zero), + confidence: Confidence::Believed, + }], + ), + ), + ( + "grant authority", + patch( + Vec::new(), + vec![ComponentOp::GrantAuthority { + holder: Ref::Existing(people[7]), + grant: AuthorityGrantRef { + kind: authority_kind(LEVY_KIND), + over: AuthorityTargetRef::Subject(Ref::Existing(people[8])), + }, + }], + ), + ), + ( + "install an incumbent", + patch( + Vec::new(), + vec![ComponentOp::InstallIncumbent { + institution: Ref::Existing(institution), + office: office(WARDEN_OFFICE), + incumbent: Ref::Existing(people[3]), + }], + ), + ), + ( + "acquire knowledge", + patch( + Vec::new(), + vec![ComponentOp::AcquireKnowledge { + subject: Ref::Existing(people[2]), + fact: Ref::Existing(fact_one), + source: AuthoredSource::Witnessed, + confidence: Confidence::Believed, + }], + ), + ), + ( + "bind a dependency", + patch( + Vec::new(), + vec![ComponentOp::Bind { + subject: Ref::Existing(people[4]), + target: DependencyRef::Subject(Ref::Existing(people[2])), + }], + ), + ), + ( + "retire a subject", + patch(Vec::new(), vec![ComponentOp::Retire { subject: Ref::Existing(people[9]) }]), + ), + ]; + let mut counts = Vec::new(); + for (shape, body) in bodies { + let rows = submit_counting_rows(&mut kernel, body); + if persons == 100 { + shapes.push(shape); + } + counts.push(rows); + } + eprintln!("{persons:>5} persons, rows per shape: {counts:?}"); + worked.push(counts); + } + for (index, shape) in shapes.iter().enumerate() { + let rows: Vec = worked.iter().map(|counts| counts[index]).collect(); + assert!( + rows.windows(2).all(|pair| pair[0] == pair[1]), + "{shape}: rows read grew with the world (100, 400, 1600): {rows:?}" + ); + } + // A shape that is point reads all the way down reads no rows at all; the + // counter must still be live, or equality proves nothing. + assert!(worked[0].iter().sum::() > 0, "no shape read a row: the counter is dead"); +} + +/// The pure reads no submit shape isolates, inside one open transaction each: +/// who stands under a two-level subtree, one incumbent's delegated authority, and +/// a path of three routes. (What a patch reads is gated through `AdmitPatch` by +/// `patch_read_work_is_equal_at_100_and_1600`.) #[test] fn operation_reads_are_equal_at_100_and_1600() { - use crate::patch::ResolvedOp; - use crate::{Cost, PressureSource}; + use crate::Cost; use std::collections::BTreeSet; let mut worked = Vec::new(); @@ -1071,24 +1279,7 @@ fn operation_reads_are_equal_at_100_and_1600() { let (zero, one, two, three) = (place("Place 0"), place("Place 1"), place("Place 2"), place("Place 3")); - // N pressure rows from other sources, committed before the measured transaction. - kernel.state.begin(); - for person in &people { - kernel.state.pressures.entry(*person).or_default().insert( - PressureSource::Subject(people[0]), - crate::patch::PressureMagnitude(1), - ); - } - let folded = kernel.state.folded().unwrap(); - kernel.state.finish(folded); - let state = &mut kernel.state; - let obligation = state.commitments[&people[0]] - .iter() - .find(|(_, commitment)| commitment.kind == CommitmentKind::Obligation) - .map(|(key, _)| *key) - .expect("the first person owes an obligation"); - state.begin(); // Place 1 inside place 0, place 2 inside place 1. state.entities.get_mut(&one).unwrap().container = Some(zero); @@ -1112,23 +1303,6 @@ fn operation_reads_are_equal_at_100_and_1600() { let mut reached = false; measure(&mut || reached = crate::action::reachable(state, people[0], &authority, three, Cost(3))); assert!(reached, "three routes of cost one reach place 3"); - let mut applied = Ok(()); - let revision = state.revision + 1; - measure(&mut || { - applied = crate::apply_operation( - state, - &ResolvedOp::DischargeCommitment { subject: people[0], key: obligation }, - &[], - revision, - ); - }); - applied.expect("the obligation is discharged"); - assert!( - state.pressures[&people[0]] - .keys() - .all(|source| !matches!(source, PressureSource::Commitment { key, .. } if *key == obligation)), - "the pressure the obligation sourced is gone" - ); state.rollback(); worked.push(counts); }