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:
+24
-5
@@ -171,7 +171,12 @@ std::uint64_t Cohort::population() const noexcept {
|
||||
return count() * residual_scale + residual();
|
||||
}
|
||||
|
||||
std::expected<void, CohortError> 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<void, CohortError> 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<void, CohortError> 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<void, CohortError> 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<void, CohortError> 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<void, CohortError> 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<void, CohortError> CohortRegistry::replace(const Cohort& cohort) {
|
||||
if (cohort.reservoir() != at->reservoir()) {
|
||||
return std::unexpected{CohortError::unknown_cohort};
|
||||
}
|
||||
*at = cohort;
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user