aetherlight: apply_operations checks conservation and disjointness from the log

Conservation sums only the holders the transaction wrote, with before taken from
each holder's value when the operations began; disjointness judges only the
holders whose grants, offices or institutions the transaction touched, through
holder_overlaps, which overlapping_holder now also calls for the open-time
whole-world check. resource_total becomes the test-only resource_total_by_scan.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
soul 2026-11-20 13:25:23 +01:00
commit b41cae3b87
2 changed files with 181 additions and 33 deletions

View file

@ -1135,6 +1135,99 @@ fn operation_reads_are_equal_at_100_and_1600() {
assert_eq!(worked[0], worked[1], "an operation's read work grew with the world");
}
/// The commit lane's checks read what the transaction wrote, not the world:
/// conservation sums the holders that were written, disjointness judges the
/// holders whose authority was written, and both cost the same rows at 100 and
/// 1600 persons. Each shape also has to be answered right: a holder already
/// written when the operations begin is measured from where it stood then, and
/// an overlap made by installing an incumbent, or by a grant, is refused with
/// every other holder untouched.
#[test]
fn apply_checks_read_the_log() {
use crate::patch::{AuthorityGrant, AuthorityTarget, ResolvedOp};
use crate::tests::{LEVY_KIND, WARDEN_OFFICE, authority_kind, office};
use crate::{Quantity, apply_operations};
let mut worked = Vec::new();
for persons in [100usize, 1600] {
let (mut kernel, people, _) = clockwork_world(persons);
let place = |label: &str| {
*kernel.state.entities.iter().find(|(_, record)| record.label == label).expect("declared").0
};
let (zero, one, grain) = (place("Place 0"), place("Place 1"), place("Grain 0"));
let institution = subject_by_label(&kernel.state, "Institution 0");
let level = AuthorityGrant {
kind: authority_kind(LEVY_KIND),
over: AuthorityTarget::PlaceSubtree(one),
};
let revision = kernel.state.revision + 1;
let transfer = ResolvedOp::Transfer { from: people[0], to: people[1], resource: grain, qty: Quantity(1) };
let mut counts = Vec::new();
let state = &mut kernel.state;
// A transfer between two of ten holders: nothing else is read.
state.begin();
let before = crate::docs::rows_read();
apply_operations(state, std::slice::from_ref(&transfer), &[], revision)
.expect("a conserving transfer applies");
counts.push(crate::docs::rows_read() - before);
state.rollback();
// A holder written before the operations begin is measured from there.
state.begin();
crate::set_held(state, people[0], grain, 5);
let before = crate::docs::rows_read();
apply_operations(state, std::slice::from_ref(&transfer), &[], revision)
.expect("the entry value is the before, not the value the transaction began with");
counts.push(crate::docs::rows_read() - before);
state.rollback();
// Place 1 inside place 0, committed, with person 5's own grant over place 1.
state.begin();
state.entities.get_mut(&one).unwrap().container = Some(zero);
state.authority.insert(people[5], std::collections::BTreeSet::from([level.clone()]));
let folded = state.folded().unwrap();
state.finish(folded);
crate::journal::verify_state_shape(state).expect("the world is still disjoint");
// A grant that overlaps the warden's lent ground is refused.
state.begin();
let overlap = ResolvedOp::GrantAuthority { holder: people[0], grant: level.clone() };
let before = crate::docs::rows_read();
let refused = apply_operations(state, &[overlap], &[], revision).unwrap_err();
counts.push(crate::docs::rows_read() - before);
assert!(
matches!(&refused, crate::KernelError::Invariant(m) if m.contains("overlapping")),
"{persons}: {refused:?}"
);
state.rollback();
// Seating a holder of overlapping ground in the office is refused too.
state.begin();
let install = ResolvedOp::InstallIncumbent {
institution,
office: office(WARDEN_OFFICE),
incumbent: people[5],
};
let before = crate::docs::rows_read();
let refused = apply_operations(state, &[install], &[], revision).unwrap_err();
counts.push(crate::docs::rows_read() - before);
assert!(
matches!(&refused, crate::KernelError::Invariant(m) if m.contains("overlapping")),
"{persons}: {refused:?}"
);
state.rollback();
// Custody is point reads, which are free; the disjointness judgments read
// the office index. A refusal stops at the first overlapping holder, and
// which holder that is depends on id order, so its reads are bounded
// rather than equal: a few rows, where a scan of the world reads thousands.
assert!(counts[2..].iter().all(|rows| (1..=8).contains(rows)), "{persons}: {counts:?}");
worked.push(counts[..2].to_vec());
}
assert_eq!(worked[0], worked[1], "a commit check's read work grew with the world");
}
fn assert_indexes_derived(state: &crate::WorldState, label: &str) {
for (schema, docs) in state.partitions() {
assert!(docs.index_is_derived(), "{label}: the read index of {schema} left its documents");

View file

@ -1685,16 +1685,17 @@ fn admit_resolved(
apply_operations(state, &resolved.operations, &resolved.evidence, resolves_to)
}
/// The component writer for every lane: snapshot the before-totals for every
/// resource the operations name, apply each operation, accumulate the ledger
/// deltas, and prove conservation once. Declaration admission calls it after its
/// declaration loops; an exercised affordance's lowered effects call it
/// directly. One writer, one conservation statement.
/// The component writer for every lane: apply each operation, accumulate the
/// ledger deltas, and prove conservation once. Declaration admission calls it
/// after its declaration loops; an exercised affordance's lowered effects call
/// it directly. One writer, one conservation statement.
///
/// A forged effect reaches this function without ever passing the resolver, so
/// conservation is re-derived over the committed partitions, against the same
/// equation, with `before` read from state rather than from anything the effect
/// asserts.
/// conservation and disjointness are re-derived here, from the transaction's own
/// log: only a holder the operations wrote can change either answer, because
/// every other holder appears on both sides of the equation and no other
/// jurisdiction moved. `before` is what each written holder held when this
/// function was entered, never anything the effect asserts.
fn apply_operations(
state: &mut WorldState,
operations: &[ResolvedOp],
@ -1704,9 +1705,16 @@ fn apply_operations(
let mut deltas: BTreeMap<EntityId, LedgerDelta> = BTreeMap::new();
for operation in operations {
for resource in operation_resources(operation) {
deltas.entry(resource).or_default().before = resource_total(state, resource);
deltas.entry(resource).or_default();
}
}
// Holders already written when the operations begin (the declarations
// above, normally none): what they hold now is their `before`.
let at_entry: BTreeMap<SubjectId, Option<BTreeMap<EntityId, Quantity>>> = state
.holdings
.changes()
.map(|(holder, _, now)| (*holder, now.cloned()))
.collect();
for operation in operations {
apply_operation(state, operation, evidence, resolves_to)?;
match operation {
@ -1731,15 +1739,25 @@ fn apply_operations(
_ => {}
}
}
for (resource, delta) in &mut deltas {
delta.after = resource_total(state, *resource);
let held_of = |held: Option<&BTreeMap<EntityId, Quantity>>, resource: &EntityId| {
held.and_then(|held| held.get(resource)).map_or(0, |quantity| u128::from(quantity.0))
};
for (holder, logged, now) in state.holdings.changes() {
let before = match at_entry.get(holder) {
Some(recorded) => recorded.as_ref(),
None => logged,
};
for (resource, delta) in &mut deltas {
delta.before += held_of(before, resource);
delta.after += held_of(now, resource);
}
}
if patch::check_ledger(&deltas).is_some() {
return Err(KernelError::Invariant("custody does not conserve".into()));
}
// Disjointness, re-derived over the committed partitions for the same
// reason conservation is: a forged effect reaches this function without
// passing the resolver.
// Disjointness, re-derived for the same reason conservation is: a forged
// effect reaches this function without passing the resolver. Only a holder
// the transaction touched can have gained an overlap.
if operations.iter().any(|operation| {
matches!(
operation,
@ -1747,7 +1765,7 @@ fn apply_operations(
| ResolvedOp::OpenOffice { .. }
| ResolvedOp::InstallIncumbent { .. }
)
}) && overlapping_holder(state).is_some()
}) && touched_holders(state).into_iter().any(|holder| holder_overlaps(state, holder))
{
return Err(KernelError::Invariant(
"one subject holds two overlapping jurisdictions of one kind".into(),
@ -1756,6 +1774,37 @@ fn apply_operations(
Ok(())
}
/// Every subject whose effective authority the open transaction may have
/// changed: whose own grants were written, whose offices were written (old
/// incumbent and new), and the incumbents of an institution whose grants were
/// written or whose retirement flipped, because an office lends what its
/// institution holds.
fn touched_holders(state: &WorldState) -> BTreeSet<SubjectId> {
let mut touched = BTreeSet::new();
let mut institutions = BTreeSet::new();
for (holder, _, _) in state.authority.changes() {
touched.insert(*holder);
institutions.insert(*holder);
}
for (institution, old, new) in state.selection.changes() {
for offices in [old, new].into_iter().flatten() {
touched.extend(offices.values().filter_map(|office| office.incumbent));
}
institutions.insert(*institution);
}
for (subject, old, new) in state.subjects.changes() {
if old.map(|record| record.retired) != new.map(|record| record.retired) {
institutions.insert(*subject);
}
}
for institution in institutions {
if let Some(offices) = state.selection.get(&institution) {
touched.extend(offices.values().filter_map(|office| office.incumbent));
}
}
touched
}
/// Every resource an operation names, so `admit_resolved` can read its committed
/// total before touching it.
fn operation_resources(operation: &ResolvedOp) -> Vec<EntityId> {
@ -1773,7 +1822,10 @@ fn operation_resources(operation: &ResolvedOp) -> Vec<EntityId> {
}
}
fn resource_total(state: &WorldState, resource: EntityId) -> u128 {
/// What `apply_operations` sums before and after: every holder. Conservation
/// reads only the holders a transaction wrote, and this is the reference.
#[cfg(test)]
fn resource_total_by_scan(state: &WorldState, resource: EntityId) -> u128 {
state
.holdings
.values()
@ -2944,14 +2996,17 @@ fn overlapping_holder(state: &WorldState) -> Option<SubjectId> {
.filter_map(|office| office.incumbent),
)
.collect();
holders.into_iter().find(|holder| {
let effective: Vec<AuthorityGrant> =
subject_authority(state, *holder).into_iter().collect();
effective.iter().enumerate().any(|(index, one)| {
effective[index + 1..].iter().any(|other| {
one.kind == other.kind
&& patch::targets_overlap(&state.entities, one.over, other.over)
})
holders.into_iter().find(|holder| holder_overlaps(state, *holder))
}
/// Whether one subject's effective authority holds two grants of one kind over
/// overlapping ground. The pairwise check `overlapping_holder` runs over every
/// holder, and `apply_operations` over the holders a transaction touched.
fn holder_overlaps(state: &WorldState, holder: SubjectId) -> bool {
let effective: Vec<AuthorityGrant> = subject_authority(state, holder).into_iter().collect();
effective.iter().enumerate().any(|(index, one)| {
effective[index + 1..].iter().any(|other| {
one.kind == other.kind && patch::targets_overlap(&state.entities, one.over, other.over)
})
})
}
@ -6157,7 +6212,7 @@ mod custody_tests {
assert_eq!(holding(&kernel, custody.holder, custody.tithe), 4);
assert_eq!(holding(&kernel, custody.counterparty, custody.tithe), 3);
assert_eq!(resource_total(&kernel.state, custody.tithe), 7);
assert_eq!(resource_total_by_scan(&kernel.state, custody.tithe), 7);
assert_eq!(kernel.state.entities, entities);
assert_eq!(kernel.state.edges, edges);
assert_eq!(kernel.state.positions, positions);
@ -6221,8 +6276,8 @@ mod custody_tests {
assert_eq!(holding(&kernel, custody.holder, custody.tithe), 2);
assert_eq!(holding(&kernel, custody.holder, custody.ingot), 5);
assert_eq!(resource_total(&kernel.state, custody.tithe), 2);
assert_eq!(resource_total(&kernel.state, custody.ingot), 5);
assert_eq!(resource_total_by_scan(&kernel.state, custody.tithe), 2);
assert_eq!(resource_total_by_scan(&kernel.state, custody.ingot), 5);
}
#[test]
@ -6494,13 +6549,13 @@ mod custody_tests {
// The opening balance was created by `Admit` alone: before is zero,
// admitted is the balance, and the total equals the sum of the two.
assert_eq!(
resource_total(&kernel.state, custody.tithe),
resource_total_by_scan(&kernel.state, custody.tithe),
u128::from(OPENING_BALANCE)
);
assert_eq!(resource_total(&kernel.state, custody.ingot), 0);
assert_eq!(resource_total_by_scan(&kernel.state, custody.ingot), 0);
let tithe_before = resource_total(&kernel.state, custody.tithe);
let ingot_before = resource_total(&kernel.state, custody.ingot);
let tithe_before = resource_total_by_scan(&kernel.state, custody.tithe);
let ingot_before = resource_total_by_scan(&kernel.state, custody.ingot);
let receipt = submit_play(
&mut kernel,
@ -6530,11 +6585,11 @@ mod custody_tests {
// Transfer contributes to no term; transform contributes one spent and
// one gained; consume contributes one consumed. Nothing was admitted.
assert_eq!(
resource_total(&kernel.state, custody.tithe),
resource_total_by_scan(&kernel.state, custody.tithe),
tithe_before - 1 - 2
);
assert_eq!(
resource_total(&kernel.state, custody.ingot),
resource_total_by_scan(&kernel.state, custody.ingot),
ingot_before + 2
);
assert_eq!(holding(&kernel, custody.holder, custody.tithe), 2);