From 14e0c1af5ea0f007a8878e78dca207afc8bbf20e Mon Sep 17 00:00:00 2001 From: dingo Date: Thu, 24 Sep 2026 17:47:37 -0400 Subject: [PATCH] An empty reservoir is not an unowned one Two defects from review of 380d609. 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 in 1a59b63. 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 --- docs/STATUS.md | 20 +++ docs/adr/0037-collapse-to-cohort.md | 16 +++ docs/adr/0039-materialize-representatives.md | 16 ++- docs/biology/ECOLOGY_MODEL.md | 12 ++ docs/engineering/VALIDATION.md | 50 ++++++++ include/evo/ecology/cohort.hpp | 11 ++ include/evo/ecology/collapse.hpp | 8 ++ include/evo/ecology/materialize.hpp | 8 +- src/ecology/cohort.cpp | 29 ++++- src/ecology/collapse.cpp | 29 ++++- src/ecology/materialize.cpp | 12 ++ tests/collapse_tests.cpp | 32 +++++ tests/materialize_tests.cpp | 126 +++++++++++++++---- 13 files changed, 332 insertions(+), 37 deletions(-) diff --git a/docs/STATUS.md b/docs/STATUS.md index da1c6ed..109f6ff 100644 --- a/docs/STATUS.md +++ b/docs/STATUS.md @@ -448,3 +448,23 @@ Eleven guards were confirmed load-bearing by removing each and observing a faili run, including the two that are the risk itself: rounding shares up, and drawing ages from the bottom of a class. Native Release, analysis, ASan and formatting passed 37/37; Linux CI has not run yet. + +Review of `380d609` found two defects, both fixed with regressions confirmed to +fail without them. Materialization treated an empty reservoir as an available one, +so it credited a store that another installed cohort owned while leaving that +cohort's record unchanged: the same inventory in two places, reached from the +opposite direction to the M4.2 defect. Destinations are now checked against the +registry's ownership as well as their balance, on both the collapse and the +materialize side. The collapse side had the worse form of it: the shared-reservoir +rule would have refused the install only after the transfer had run, stranding the +members' biomass in a reservoir nothing accounted for, so whichever registry step +will follow a transfer is now proved to succeed before it. + +The second was a functional gap the oscillation test had been hiding 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, making a full materialization a one-way door out of a habitat. +Collapse now repopulates a vacant record in place, continuing its ownership and +revision series rather than restarting them, and refuses a record that still holds +a population. The test keeps one registry throughout and asserts the revision +continues. diff --git a/docs/adr/0037-collapse-to-cohort.md b/docs/adr/0037-collapse-to-cohort.md index ddd4e76..4d6c641 100644 --- a/docs/adr/0037-collapse-to-cohort.md +++ b/docs/adr/0037-collapse-to-cohort.md @@ -70,6 +70,22 @@ carries a cohort already validated and the destination was already proved available. Every rejection therefore leaves the ledger and the registry exactly as they were. +Two things were added here after review, and both are corrections rather than +refinements. A destination reservoir that another cohort owns is refused, because +emptiness is not availability: a vacant cohort still owns its store, and the +shared-reservoir rule would otherwise have refused the install only after the +transfer had run, leaving the members emptied and their biomass in a reservoir +nothing accounts for. Whichever registry step will follow the transfer is now +checked before it, through the registry rather than by restating its rules. + +And a habitat and genome already holding a vacant record is repopulated rather than +refused. ADR-0036 keeps a record whose population reached zero because absence is +not extinction, so without this a lineage could never return to a habitat it had +once emptied, and a full materialization would be a one-way door. The retained +record's ownership and revision series continue: the plan has to be a strictly +later view of it, which is the rule every other replacement obeys. A record that is +not vacant is a population, and merging two silently is not a conversion. + The reservoir's emptiness is checked after the sample rather than beside the other destination checks. Both run before anything moves, so this is a reporting choice: when a committed plan is replayed into a fresh registry both would refuse it, and diff --git a/docs/adr/0039-materialize-representatives.md b/docs/adr/0039-materialize-representatives.md index ccf2470..d643484 100644 --- a/docs/adr/0039-materialize-representatives.md +++ b/docs/adr/0039-materialize-representatives.md @@ -61,6 +61,14 @@ member's reservoir must be empty first, so a member's reserve is the whole of wh backs it rather than a part sharing a store with inventory nobody counted. That is the same rule the destination of a collapse has. +Emptiness is not availability, and that distinction had to be added after review. A +vacant cohort, or one simply holding nothing, still owns its reservoir, so crediting +it would put inventory there that the owning record does not account for: the same +inventory in two places, arrived at from the other direction. Every destination is +therefore checked against the registry's ownership as well as against its balance, +and the registry answers whether a reservoir is used rather than callers guessing +from what it contains. + What the resolution discards is stated, not mitigated. A round trip is exact when each class holds one member and smooths within a class otherwise, and the tests demonstrate both rather than only the flattering one. The available mitigation is @@ -123,8 +131,10 @@ many cycles: the conserved total never moves, no member ever holds more than the largest starting member held, and ages never decrease. Test scarcity, where a class holding nothing materializes members holding nothing rather than refusing or inventing. Test that drawing more than a class holds, an unknown or repeated class, -a reservoir that is not empty, a repeated reservoir, a stale view and a wrong owner -are each refused, and that every rejection leaves the ledger and the registry -unchanged. No genome schema, mutation catalog, archive, ledger, episode driver, +a reservoir that is not empty, a reservoir another cohort owns however empty, a +repeated reservoir, a stale view and a wrong owner are each refused, and that every +rejection leaves the ledger and the registry unchanged. Test the round trip through +one registry that is never discarded, so a full draw and the collapse that follows +it have to work against the record the draw left behind. No genome schema, mutation catalog, archive, ledger, episode driver, ensemble, cohort record, collapse, interval accounting, renderer or deployment changes. diff --git a/docs/biology/ECOLOGY_MODEL.md b/docs/biology/ECOLOGY_MODEL.md index 7d70725..d3a44d2 100644 --- a/docs/biology/ECOLOGY_MODEL.md +++ b/docs/biology/ECOLOGY_MODEL.md @@ -258,6 +258,18 @@ any individual is right. A round trip is exact when each class holds one member, and the mitigation available to a caller is to declare narrower classes when it collapses; nothing here can recover what a coarse class threw away. +A destination reservoir must be unowned as well as empty. A vacant cohort, or one +holding nothing, still owns its store, so crediting it would put inventory there +that the owning record does not account for. This holds on both sides of the +conversion, and the registry is asked rather than inferred from what a reservoir +contains. + +A habitat and genome holding a vacant record is repopulated rather than refused, +because this document requires the record to be retained and a retained record +would otherwise bar the lineage from ever returning. The repopulation continues the +retained record's ownership and revision series; it does not reset them, and a +record that still holds a population is never merged into silently. + A materialized member carries the cohort's genome, its habitat and a fresh identity, and nothing else. It has no parentage, no history and no condition, because a cohort never carried them, and none of that may later be invented from a diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index e3c371c..a3f1bc9 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -1192,3 +1192,53 @@ fingerprint `2cd4a15f9fbe9949`, the ensemble fingerprint `5ec59d390909b526` and the frozen genome fixture are unchanged, and the other 36 tests pass untouched. No deployment manifest or image pin changed. Linux validation is pending; no Gitea result is claimed since `b01a824`. + +Review of `380d609` found two defects (2026-09-24), both fixed with regressions +confirmed to fail against the unfixed code. + +Materialization treated an empty reservoir as an available one. A vacant cohort, or +one simply holding nothing, still owns its store, so a draw could credit that store +while the owning record went on recording nothing: the same inventory in two places, +reached from the opposite direction to the destination defect found in `1a59b63`. +Destinations are now checked against the registry's ownership as well as their +balance, and the registry answers whether a reservoir is used rather than callers +inferring availability from what it contains. + +The same hole existed on the collapse side in a worse form, and was found by +checking whether the reported defect had a sibling rather than by being reported. +Nothing verified that the destination reservoir was unowned, so the shared-reservoir +rule added in M4.3 would have refused the install only after the ledger transfer had +already 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 will follow a transfer +is now proved to succeed before it, through new `may_install` and `may_replace` +queries that `install` and `replace` themselves delegate to, so there is one rule +rather than a restatement that could drift. + +The second finding was a functional gap the oscillation test had been hiding by +discarding its registry every 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 therefore a one-way door +out of a habitat: the lineage that had been there could never return. Collapse now +repopulates a vacant record in place, continuing its ownership and revision series +rather than restarting them, and a record that still holds a population is still +refused, because merging two silently is not a conversion. The oscillation test keeps +one registry for all twelve cycles and asserts the revision continues across each +repopulation, so the round trip is exercised against the world it left behind. + +Four of the five new guards were confirmed load-bearing by removing each alone: the +ownership check on both sides, the collapse pre-check ordering, and the repopulation +path. The fifth is not, and is recorded rather than presented as tested. The +`may_replace` pre-check in `commit_materialize` is unreachable given the checks +before it: the installed record has already been proved equal to the one the plan +was built from, and the plan's result carries the same habitat, genome, reservoir and +tick with a strictly later revision, so the replacement cannot be refused. Removing +it changes nothing the tests can see. It is kept because it is the structurally +correct place for the guard and its sibling on the collapse path is genuinely +reachable, not because it is exercised. + +An environment note for the record: a Bash heredoc containing apostrophes failed to +parse, so the larger test edits in this increment were written to a scratchpad file +and run from there. That is the third distinct way heredocs have mangled content in +this project, after backslash escapes and `/tmp` resolving differently between +shells. diff --git a/include/evo/ecology/cohort.hpp b/include/evo/ecology/cohort.hpp index 4c585b5..3c68349 100644 --- a/include/evo/ecology/cohort.hpp +++ b/include/evo/ecology/cohort.hpp @@ -197,6 +197,17 @@ class Cohort { // forbidden. Nothing is serialized and no cross-worker protocol is implied. class CohortRegistry { public: + // Whether installing or replacing would be accepted, without doing it. A + // conversion has to know before it moves any inventory, because a registry + // step that failed afterwards would leave biomass in a store nothing owns. + // `install` and `replace` delegate to these, so there is one rule and not two. + [[nodiscard]] std::expected may_install(const Cohort& cohort) const; + [[nodiscard]] std::expected may_replace(const Cohort& cohort) const; + // Whether any installed record's reserve lives in this reservoir. An empty + // reservoir is not the same as an unowned one: a vacant cohort, or one holding + // nothing, still owns its store, and crediting it would put inventory there + // that its own record does not account for. + [[nodiscard]] bool reservoir_used(accounting::ReservoirId reservoir) const noexcept; // Also refuses a cohort whose reservoir another cohort already uses: one // reservoir backing two records is the same inventory in two places, which is // the invariant this registry exists for. diff --git a/include/evo/ecology/collapse.hpp b/include/evo/ecology/collapse.hpp index 389d407..4f7ea0d 100644 --- a/include/evo/ecology/collapse.hpp +++ b/include/evo/ecology/collapse.hpp @@ -68,6 +68,7 @@ enum class CollapseError : std::uint8_t { stale_plan, destination_occupied, destination_not_empty, + destination_owned, destination_unavailable, ledger_failure }; @@ -126,6 +127,13 @@ plan_collapse(std::span members, const CollapseRequest& requ // anything moves, so no rejection can strand inventory in a reservoir nothing owns // and the cohort's reserve is exactly what its reservoir holds: every failure // leaves the ledger and the registry as they were. +// +// Where the habitat and genome already hold a vacant record, this repopulates it +// rather than refusing. ADR-0036 keeps a record whose population reached zero, +// because absence is not extinction, so without this a habitat could never be +// recolonised by the lineage that had been there. The retained record's ownership +// and revision series continue: the plan has to be a strictly later view of it, +// which is the same rule any other replacement obeys. [[nodiscard]] std::expected commit_collapse(accounting::Ledger& ledger, CohortRegistry& registry, const CollapsePlan& plan); } // namespace evo::ecology diff --git a/include/evo/ecology/materialize.hpp b/include/evo/ecology/materialize.hpp index 8b50439..6ba12dc 100644 --- a/include/evo/ecology/materialize.hpp +++ b/include/evo/ecology/materialize.hpp @@ -41,6 +41,9 @@ enum class MaterializeError : std::uint8_t { not_installed, stale_plan, reservoir_not_empty, + // An empty reservoir is not an available one: a vacant cohort, or one holding + // nothing, still owns its store. + reservoir_owned, ledger_failure }; @@ -91,8 +94,9 @@ class MaterializePlan { plan_materialize(const Cohort& before, const MaterializeRequest& request); // Applies exactly that plan. The installed record must still be the one the plan -// started from and every destination reservoir must be empty, so no rejection can -// leave the cohort debited with nobody holding what it gave up. +// started from, and every destination reservoir must be both empty and unowned, so +// no rejection can leave the cohort debited with nobody holding what it gave up and +// no credit can land in a store some other record already accounts for. [[nodiscard]] std::expected, MaterializeError> commit_materialize(accounting::Ledger& ledger, CohortRegistry& registry, const MaterializePlan& plan); diff --git a/src/ecology/cohort.cpp b/src/ecology/cohort.cpp index 0fa2a9e..d867d5c 100644 --- a/src/ecology/cohort.cpp +++ b/src/ecology/cohort.cpp @@ -171,7 +171,12 @@ std::uint64_t Cohort::population() const noexcept { return count() * residual_scale + residual(); } -std::expected CohortRegistry::install(const Cohort& cohort) { +bool CohortRegistry::reservoir_used(accounting::ReservoirId reservoir) const noexcept { + return std::ranges::any_of(cohorts_, + [&](const Cohort& other) { return other.reservoir() == reservoir; }); +} + +std::expected CohortRegistry::may_install(const Cohort& cohort) const { const auto wanted = key_of(cohort); const auto at = std::ranges::lower_bound(cohorts_, wanted, {}, key_of); if (at != cohorts_.end() && key_of(*at) == wanted) { @@ -179,19 +184,34 @@ std::expected CohortRegistry::install(const Cohort& cohort) { } // One reservoir backing two records would be the same inventory in two // places, which is what this registry exists to make unrepresentable. - if (std::ranges::any_of(cohorts_, [&](const Cohort& other) { - return other.reservoir() == cohort.reservoir(); - })) { + if (reservoir_used(cohort.reservoir())) { return std::unexpected{CohortError::duplicate_cohort}; } if (cohorts_.size() >= max_cohorts) { return std::unexpected{CohortError::registry_full}; } + return {}; +} + +std::expected CohortRegistry::install(const Cohort& cohort) { + if (const auto allowed = may_install(cohort); !allowed) { + return allowed; + } + const auto at = std::ranges::lower_bound(cohorts_, key_of(cohort), {}, key_of); cohorts_.insert(at, cohort); return {}; } std::expected CohortRegistry::replace(const Cohort& cohort) { + if (const auto allowed = may_replace(cohort); !allowed) { + return allowed; + } + const auto at = std::ranges::lower_bound(cohorts_, key_of(cohort), {}, key_of); + *at = cohort; + return {}; +} + +std::expected CohortRegistry::may_replace(const Cohort& cohort) const { const auto wanted = key_of(cohort); const auto at = std::ranges::lower_bound(cohorts_, wanted, {}, key_of); // Habitat and genome are the record's membership, so a replacement changing @@ -224,7 +244,6 @@ std::expected CohortRegistry::replace(const Cohort& cohort) { if (cohort.reservoir() != at->reservoir()) { return std::unexpected{CohortError::unknown_cohort}; } - *at = cohort; return {}; } diff --git a/src/ecology/collapse.cpp b/src/ecology/collapse.cpp index 7ce44e5..4fbb1d5 100644 --- a/src/ecology/collapse.cpp +++ b/src/ecology/collapse.cpp @@ -146,11 +146,30 @@ commit_collapse(accounting::Ledger& ledger, CohortRegistry& registry, const Coll // more than "the world moved". Both are pure, so the order is a reporting // choice and not a safety one. const auto& cohort = plan.cohort(); - if (registry.find(cohort.habitat(), cohort.genome()).has_value()) { + const auto standing = registry.find(cohort.habitat(), cohort.genome()); + // A vacant record is repopulated rather than refused: ADR-0036 keeps a record + // whose population reached zero, so without this the lineage that had been + // here could never come back. Anything else already holding this habitat and + // genome is a population, and merging two silently is not a conversion. + const auto repopulating = standing.has_value() && standing->is_vacant(); + if (standing.has_value() && !repopulating) { return std::unexpected{CollapseError::destination_occupied}; } - if (registry.size() >= max_cohorts) { - return std::unexpected{CollapseError::destination_unavailable}; + // Whichever step will follow the transfer, it has to be known to succeed now. + // A registry step that failed afterwards would leave the members emptied and + // their inventory in a store nothing accounts for. + const auto allowed = repopulating ? registry.may_replace(cohort) : registry.may_install(cohort); + if (!allowed) { + if (allowed.error() == CohortError::registry_full) { + return std::unexpected{CollapseError::destination_unavailable}; + } + // An empty reservoir is not an available one. A vacant cohort, or one + // holding nothing, still owns its store, and gathering into it would put + // inventory there that its own record does not account for. + if (!repopulating && registry.reservoir_used(plan.destination())) { + return std::unexpected{CollapseError::destination_owned}; + } + return std::unexpected{CollapseError::destination_occupied}; } // The sample has to still be true. Holding anything other than exactly what // was sampled means the world moved, and moving only the sampled part would @@ -178,7 +197,9 @@ commit_collapse(accounting::Ledger& ledger, CohortRegistry& registry, const Coll if (!ledger.transfer(ledger.last_action() + 1, plan.legs())) { return std::unexpected{CollapseError::ledger_failure}; } - if (!registry.install(cohort)) { + // Proved above, so neither of these can fail after the transfer. + const auto settled = repopulating ? registry.replace(cohort) : registry.install(cohort); + if (!settled) { return std::unexpected{CollapseError::destination_occupied}; } return cohort; diff --git a/src/ecology/materialize.cpp b/src/ecology/materialize.cpp index 055a220..8f66a35 100644 --- a/src/ecology/materialize.cpp +++ b/src/ecology/materialize.cpp @@ -216,6 +216,18 @@ commit_materialize(accounting::Ledger& ledger, CohortRegistry& registry, if (!empty(balances[member.reservoir.value - 1])) { return std::unexpected{MaterializeError::reservoir_not_empty}; } + // Emptiness is not availability. A vacant cohort, or one holding nothing, + // still owns its reservoir, and crediting it would put inventory there + // that the owning record does not account for, which is the same + // inventory in two places. + if (registry.reservoir_used(member.reservoir)) { + return std::unexpected{MaterializeError::reservoir_owned}; + } + } + // Whatever follows the transfer must be known to succeed now, or a failure + // would leave the cohort debited with nobody holding what it gave up. + if (!registry.may_replace(plan.after())) { + return std::unexpected{MaterializeError::stale_plan}; } if (!plan.legs().empty()) { if (ledger.last_action() == maximum) { diff --git a/tests/collapse_tests.cpp b/tests/collapse_tests.cpp index e5ad7d8..b89d697 100644 --- a/tests/collapse_tests.cpp +++ b/tests/collapse_tests.cpp @@ -310,6 +310,37 @@ void a_destination_holding_anything_is_refused() { CHECK(ledger.balances()[4] == installed->reserve()); } +void a_destination_owned_by_another_cohort_is_refused() { + // An empty reservoir is not an available one. A cohort holding nothing still + // owns its store, and gathering into it would put inventory there that its own + // record does not account for. Worse, the shared-reservoir rule would have + // refused the install only after the transfer, leaving the members emptied and + // their biomass in a reservoir nothing accounts for. + auto ledger = world(); + CohortRegistry registry; + CohortDraft squatter; + squatter.representation_version = cohort_representation_version; + squatter.habitat = {7}; + squatter.genome = 99; // A different lineage, so not the habitat/genome guard. + squatter.owner = {11, 2}; + squatter.last_tick = evo::Tick{900}; + squatter.revision = 1; + squatter.conversion = 1; + squatter.reservoir = destination; // The reservoir this collapse would gather into. + squatter.classes = {{0, 100, 1, Inventory{}, 0}}; + const auto sitting = Cohort::create(squatter); + CHECK(sitting.has_value()); + CHECK(registry.install(*sitting).has_value()); + const auto plan = planned(); + const auto opening = std::vector{ledger.balances().begin(), ledger.balances().end()}; + const auto action = ledger.last_action(); + CHECK(refused(commit_collapse(ledger, registry, plan), CollapseError::destination_owned)); + CHECK(std::vector(ledger.balances().begin(), ledger.balances().end()) == opening); + CHECK(ledger.last_action() == action); + CHECK(registry.size() == 1); + CHECK(registry.find(HabitatId{7}, 99)->reserve() == Inventory{}); +} + void one_plan_cannot_be_committed_twice() { auto ledger = world(); CohortRegistry registry; @@ -368,6 +399,7 @@ int main() try { a_committed_collapse_conserves_and_empties_the_members(); every_rejection_leaves_the_world_as_it_was(); a_destination_holding_anything_is_refused(); + a_destination_owned_by_another_cohort_is_refused(); one_plan_cannot_be_committed_twice(); a_moved_from_plan_is_still_a_whole_plan(); std::cout << "PASS collapse sampling, exact conservation and unchanged-on-rejection\n"; diff --git a/tests/materialize_tests.cpp b/tests/materialize_tests.cpp index 91a22ed..4d24b0e 100644 --- a/tests/materialize_tests.cpp +++ b/tests/materialize_tests.cpp @@ -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 -[[nodiscard]] bool refused(const std::expected& result, - MaterializeError expected) { +template +[[nodiscard]] bool refused(const std::expected& 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 opening{}; std::vector 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 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 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 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 drain; - for (std::size_t index = 0; index < members.size(); ++index) { - const ReservoirId fresh{static_cast(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{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(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";