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:
@@ -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<void, CohortError> may_install(const Cohort& cohort) const;
|
||||
[[nodiscard]] std::expected<void, CohortError> 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.
|
||||
|
||||
@@ -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<const ActiveMember> 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<Cohort, CollapseError>
|
||||
commit_collapse(accounting::Ledger& ledger, CohortRegistry& registry, const CollapsePlan& plan);
|
||||
} // namespace evo::ecology
|
||||
|
||||
@@ -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<std::vector<ActiveMember>, MaterializeError>
|
||||
commit_materialize(accounting::Ledger& ledger, CohortRegistry& registry,
|
||||
const MaterializePlan& plan);
|
||||
|
||||
Reference in New Issue
Block a user