An empty reservoir is not an unowned one
Two defects from review of380d609. Materialization treated an empty reservoir as available. A vacant cohort, or one holding nothing, still owns its store, so a draw could credit it while the owning record went on recording nothing: the same inventory in two places, reached from the opposite direction to the destination defect in1a59b63. Destinations are checked against the registry's ownership now, not just their balance, and the registry answers whether a reservoir is used rather than callers inferring it from what the reservoir contains. The same hole existed on the collapse side in a worse form, found by asking whether the reported defect had a sibling. Nothing verified the destination was unowned, so the shared-reservoir rule added in M4.3 would have refused the install only after the transfer had run, leaving the members emptied and their biomass in a reservoir nothing accounted for. That is exactly the stranding the M4.2 ordering was chosen to prevent, defeated by a check that ran too late. Whichever registry step follows a transfer is now proved to succeed before it, through may_install and may_replace queries that install and replace delegate to, so there is one rule and not a restatement that can drift. The second finding was a functional gap the oscillation test hid by discarding its registry each cycle. A full draw leaves a vacant record, which ADR-0036 retains because absence is not extinction, and collapse then refused that habitat and genome as occupied: a full materialization was a one-way door out of a habitat and the lineage could never return. Collapse repopulates a vacant record in place now, continuing its ownership and revision series rather than restarting them, and a record still holding a population is refused, because merging two silently is not a conversion. The test keeps one registry for all twelve cycles and asserts the revision continues. Four of the five new guards are load-bearing. The fifth is not and is recorded as such: the may_replace pre-check in commit_materialize is unreachable given the checks before it, since the installed record is already proved equal to the plan's origin and the result differs only by a later revision. It is kept because it is the right place for the guard and its collapse-side sibling is genuinely reachable, not because it is exercised. Release 37/37, analysis, ASan 37/37 and clang-format all ran and passed here. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
+103
-23
@@ -25,9 +25,8 @@ void check(bool condition, std::source_location location = std::source_location:
|
||||
|
||||
// Asserting on .error() alone is undefined when the call unexpectedly succeeded,
|
||||
// which would let a broken guard pass quietly. This checks the failure first.
|
||||
template <typename T>
|
||||
[[nodiscard]] bool refused(const std::expected<T, MaterializeError>& result,
|
||||
MaterializeError expected) {
|
||||
template <typename T, typename E>
|
||||
[[nodiscard]] bool refused(const std::expected<T, E>& result, E expected) {
|
||||
return !result.has_value() && result.error() == expected;
|
||||
}
|
||||
|
||||
@@ -280,10 +279,11 @@ void a_coarse_class_smooths_and_the_loss_is_not_hidden() {
|
||||
}
|
||||
|
||||
void repeated_oscillation_never_pays() {
|
||||
// Collapse and materialize over and over. The conserved total must not move,
|
||||
// no member may ever hold more than the largest starting member held, and
|
||||
// nobody may get younger. Those three together are what stops a round trip
|
||||
// being a way to heal or to reset age.
|
||||
// Collapse and materialize over and over, through one registry that is never
|
||||
// discarded, so a round trip has to work against the world it left behind.
|
||||
// The conserved total must not move, no member may ever hold more than the
|
||||
// largest starting member held, and nobody may get younger. Those three
|
||||
// together are what stops a round trip being a way to heal or to reset age.
|
||||
std::array<Inventory, reservoir_count> opening{};
|
||||
std::vector<ActiveMember> members{{1, 3, {7}, 10, {2}, held(90, 900), MemberState::active},
|
||||
{2, 3, {7}, 40, {3}, held(10, 100), MemberState::active},
|
||||
@@ -295,11 +295,11 @@ void repeated_oscillation_never_pays() {
|
||||
CHECK(ledger.has_value());
|
||||
const auto total = ledger->conserved_totals();
|
||||
const auto ceiling = held(90, 900);
|
||||
CohortRegistry registry;
|
||||
std::uint64_t identity = 100;
|
||||
std::uint64_t revision = 1;
|
||||
std::vector<std::uint64_t> ages{10, 40, 150};
|
||||
for (std::uint64_t cycle = 0; cycle < 12; ++cycle) {
|
||||
CohortRegistry registry;
|
||||
CollapseRequest gather;
|
||||
gather.conversion = 5;
|
||||
gather.owner = {11, 2};
|
||||
@@ -312,10 +312,16 @@ void repeated_oscillation_never_pays() {
|
||||
gather.bound = {0, {}};
|
||||
const auto folded = plan_collapse(members, gather);
|
||||
CHECK(folded.has_value());
|
||||
// After the first cycle this repopulates the vacant record the previous
|
||||
// full draw left behind, continuing its revision series rather than
|
||||
// starting a new one. A fresh registry each cycle would have hidden that.
|
||||
CHECK(commit_collapse(*ledger, registry, *folded).has_value());
|
||||
CHECK(registry.size() == 1);
|
||||
CHECK(ledger->conserved_totals() == total);
|
||||
const auto cohort = registry.find(HabitatId{7}, 3);
|
||||
CHECK(cohort.has_value());
|
||||
CHECK(!cohort->is_vacant());
|
||||
CHECK(cohort->revision() == revision);
|
||||
CHECK(ledger->balances()[backing.value - 1] == cohort->reserve());
|
||||
|
||||
std::vector<ClassDraw> draws;
|
||||
@@ -332,9 +338,11 @@ void repeated_oscillation_never_pays() {
|
||||
CHECK(drawn.has_value());
|
||||
CHECK(ledger->conserved_totals() == total);
|
||||
CHECK(drawn->size() == members.size());
|
||||
// Everyone was drawn, so the retained record is vacant and still here.
|
||||
CHECK(registry.find(HabitatId{7}, 3)->is_vacant());
|
||||
CHECK(registry.size() == 1);
|
||||
std::vector<std::uint64_t> next;
|
||||
for (std::size_t index = 0; index < drawn->size(); ++index) {
|
||||
const auto& member = (*drawn)[index];
|
||||
for (const auto& member : *drawn) {
|
||||
// Nobody ever ends up richer than the richest we started with.
|
||||
CHECK(member.reserve.energy.value <= ceiling.energy.value);
|
||||
CHECK(member.reserve.matter[0].value <= ceiling.matter[0].value);
|
||||
@@ -347,26 +355,96 @@ void repeated_oscillation_never_pays() {
|
||||
CHECK(next[index] >= sorted[index]); // Nobody got younger.
|
||||
}
|
||||
ages = next;
|
||||
// Give each member back an empty store for the next cycle.
|
||||
members = *drawn;
|
||||
std::vector<Transfer> drain;
|
||||
for (std::size_t index = 0; index < members.size(); ++index) {
|
||||
const ReservoirId fresh{static_cast<std::uint32_t>(index + 2)};
|
||||
if (members[index].reservoir != fresh && !(members[index].reserve == Inventory{})) {
|
||||
drain.push_back({members[index].reservoir, fresh, members[index].reserve});
|
||||
}
|
||||
members[index].reservoir = fresh;
|
||||
}
|
||||
if (!drain.empty()) {
|
||||
CHECK(ledger->transfer(ledger->last_action() + 1, drain).has_value());
|
||||
}
|
||||
CHECK(ledger->conserved_totals() == total);
|
||||
}
|
||||
// Everyone has been pushed to a class ceiling, which is the cost of the
|
||||
// conservative age rule and is a distortion, not a model of ageing.
|
||||
for (const auto age : ages) {
|
||||
CHECK(age == 99 || age == 199);
|
||||
}
|
||||
CHECK(ledger->conserved_totals() == total);
|
||||
}
|
||||
|
||||
void an_empty_reservoir_owned_by_another_cohort_is_refused() {
|
||||
// A vacant cohort, or one holding nothing, still owns its store. Crediting it
|
||||
// would put inventory there that its own record does not account for, which
|
||||
// is the same inventory in two places.
|
||||
const auto before = standing();
|
||||
auto squatter = draft();
|
||||
squatter.genome = 4;
|
||||
squatter.reservoir = {2}; // The store the first member would be drawn into.
|
||||
squatter.classes = {{0, 100, 1, Inventory{}, 0}};
|
||||
auto registry = registry_holding(before);
|
||||
CHECK(registry.install(built(squatter)).has_value());
|
||||
CHECK(registry.reservoir_used(ReservoirId{2}));
|
||||
auto ledger = world(before);
|
||||
const auto opening = std::vector<Inventory>{ledger.balances().begin(), ledger.balances().end()};
|
||||
const auto action = ledger.last_action();
|
||||
const auto plan = plan_materialize(before, request({{0, 2}}));
|
||||
CHECK(plan.has_value());
|
||||
// Unfixed this committed, leaving the squatter recording nothing while its
|
||||
// reservoir held what had been drawn.
|
||||
CHECK(refused(commit_materialize(ledger, registry, *plan), MaterializeError::reservoir_owned));
|
||||
CHECK(std::vector<Inventory>(ledger.balances().begin(), ledger.balances().end()) == opening);
|
||||
CHECK(ledger.last_action() == action);
|
||||
CHECK(registry.size() == 2);
|
||||
CHECK(registry.find(HabitatId{7}, 4)->reserve() == Inventory{});
|
||||
CHECK(registry.find(HabitatId{7}, 3)->revision() == 4);
|
||||
// Drawing into stores nobody owns is fine.
|
||||
auto free_stores = request({{0, 2}});
|
||||
free_stores.reservoirs = {{5}, {6}};
|
||||
const auto other = plan_materialize(before, free_stores);
|
||||
CHECK(other.has_value());
|
||||
CHECK(commit_materialize(ledger, registry, *other).has_value());
|
||||
CHECK(ledger.balances()[4] == held(10, 2));
|
||||
}
|
||||
|
||||
void a_vacant_record_is_repopulated_and_keeps_its_history() {
|
||||
// Drawing everyone leaves a vacant record, and ADR-0036 keeps it because
|
||||
// absence is not extinction. Collapsing back therefore has to repopulate that
|
||||
// record rather than be refused, or the lineage could never return.
|
||||
auto whole = draft();
|
||||
whole.classes = {{0, 100, 2, held(20, 200), 0}};
|
||||
const auto before = built(whole);
|
||||
auto ledger = world(before);
|
||||
auto registry = registry_holding(before);
|
||||
const auto plan = plan_materialize(before, request({{0, 2}}));
|
||||
CHECK(plan.has_value());
|
||||
const auto drawn = commit_materialize(ledger, registry, *plan);
|
||||
CHECK(drawn.has_value());
|
||||
CHECK(registry.find(HabitatId{7}, 3)->is_vacant());
|
||||
CHECK(registry.find(HabitatId{7}, 3)->revision() == 5);
|
||||
CollapseRequest gather;
|
||||
gather.conversion = 5;
|
||||
gather.owner = {11, 2};
|
||||
gather.habitat = {7};
|
||||
gather.genome = 3;
|
||||
gather.tick = Tick{900};
|
||||
gather.revision = 6;
|
||||
gather.destination = backing;
|
||||
gather.bounds = {{0, 100}};
|
||||
const auto folded = plan_collapse(*drawn, gather);
|
||||
CHECK(folded.has_value());
|
||||
const auto again = commit_collapse(ledger, registry, *folded);
|
||||
CHECK(again.has_value());
|
||||
CHECK(registry.size() == 1); // The same record, repopulated in place.
|
||||
const auto found = registry.find(HabitatId{7}, 3);
|
||||
CHECK(found.has_value());
|
||||
CHECK(!found->is_vacant());
|
||||
CHECK(found->count() == 2);
|
||||
CHECK(found->reserve() == held(20, 200));
|
||||
// The revision series continued rather than restarting.
|
||||
CHECK(found->revision() == 6);
|
||||
CHECK(found->owner() == Owner{11, 2});
|
||||
// A record that is not vacant is a population, and merging two silently is
|
||||
// not a conversion.
|
||||
auto onto_the_living = gather;
|
||||
onto_the_living.revision = 9;
|
||||
const auto refolded = plan_collapse(*drawn, onto_the_living);
|
||||
CHECK(refolded.has_value());
|
||||
CHECK(
|
||||
refused(commit_collapse(ledger, registry, *refolded), CollapseError::destination_occupied));
|
||||
CHECK(registry.find(HabitatId{7}, 3)->count() == 2);
|
||||
}
|
||||
|
||||
void malformed_draws_and_stale_views_are_refused() {
|
||||
@@ -478,6 +556,8 @@ int main() try {
|
||||
a_round_trip_is_exact_when_a_class_holds_one_member();
|
||||
a_coarse_class_smooths_and_the_loss_is_not_hidden();
|
||||
repeated_oscillation_never_pays();
|
||||
an_empty_reservoir_owned_by_another_cohort_is_refused();
|
||||
a_vacant_record_is_repopulated_and_keeps_its_history();
|
||||
malformed_draws_and_stale_views_are_refused();
|
||||
a_moved_from_plan_is_still_a_whole_plan();
|
||||
std::cout << "PASS materialization shares, conservative ages and unprofitable round trips\n";
|
||||
|
||||
Reference in New Issue
Block a user