From 1541cb6c4fedabc1dc7bcc5f97ad3802bd585463 Mon Sep 17 00:00:00 2001 From: soul Date: Fri, 20 Nov 2026 15:53:23 +0100 Subject: [PATCH] aetherlight: apply_operations judges a holder restored to its start value The conservation sum covers the union of the holders written at entry and the holders the log reports, so a holder written before the operations and returned to its transaction-start value (absent from changes()) still counts. Co-Authored-By: Claude Sonnet 5.5 --- native/aetherlight/src/golden_tests.rs | 27 ++++++++++++++++++++++++++ native/aetherlight/src/lib.rs | 17 ++++++++++++++-- 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/native/aetherlight/src/golden_tests.rs b/native/aetherlight/src/golden_tests.rs index fa929271f..b71a3c0a7 100644 --- a/native/aetherlight/src/golden_tests.rs +++ b/native/aetherlight/src/golden_tests.rs @@ -2086,3 +2086,30 @@ fn views_equal_their_rebuild_and_the_scans_over_random_commands() { assert!(kept.get(name).copied().unwrap_or(0) > 0, "no {name} ever landed: {kept:?}"); } } + +/// A holder written before the operations begin and returned by them to the +/// value it had when the transaction opened is no longer in the log, yet it +/// moved: conservation reads it from the entry record, so the transfer that +/// restores it is judged as conserving. +#[test] +fn apply_conserves_a_holder_restored_to_its_start_value() { + use crate::patch::ResolvedOp; + use crate::{Quantity, apply_operations}; + + let (mut kernel, people, _) = clockwork_world(20); + let grain = *kernel + .state + .entities + .iter() + .find(|(_, record)| record.label == "Grain 1") + .expect("declared") + .0; + let revision = kernel.state.revision + 1; + let state = &mut kernel.state; + state.begin(); + crate::set_held(state, people[0], grain, 5); + let transfer = ResolvedOp::Transfer { from: people[0], to: people[1], resource: grain, qty: Quantity(5) }; + apply_operations(state, std::slice::from_ref(&transfer), &[], revision) + .expect("moving the pre-written five on conserves it"); + state.rollback(); +} diff --git a/native/aetherlight/src/lib.rs b/native/aetherlight/src/lib.rs index c3837d8ad..e8c28e300 100644 --- a/native/aetherlight/src/lib.rs +++ b/native/aetherlight/src/lib.rs @@ -1742,14 +1742,27 @@ fn apply_operations( let held_of = |held: Option<&BTreeMap>, resource: &EntityId| { held.and_then(|held| held.get(resource)).map_or(0, |quantity| u128::from(quantity.0)) }; + // The holders judged are the union of those written at entry and those the + // log now reports: a holder written before the operations and returned to + // its transaction-start value is absent from `changes()` but still moved. + type Held<'a> = Option<&'a BTreeMap>; + let mut judged: BTreeMap, Held<'_>)> = BTreeMap::new(); for (holder, logged, now) in state.holdings.changes() { let before = match at_entry.get(holder) { Some(recorded) => recorded.as_ref(), None => logged, }; + judged.insert(*holder, (before, now)); + } + for (holder, recorded) in &at_entry { + judged + .entry(*holder) + .or_insert_with(|| (recorded.as_ref(), state.holdings.get(holder))); + } + for (before, now) in judged.values() { for (resource, delta) in &mut deltas { - delta.before += held_of(before, resource); - delta.after += held_of(now, resource); + delta.before += held_of(*before, resource); + delta.after += held_of(*now, resource); } } if patch::check_ledger(&deltas).is_some() {