Keep a distribution, because a mean cannot say who survives
M4.1 defines the tier 2 record and who may act on it, and nothing else: no collapse transaction, no population advance, no materialization, no habitat geometry. Those are M4.2 to M4.4 and each needs its own decision. The named risk for this step is that population averages hide viable outliers, which is a property of the record rather than of the maths applied to it later, so it is answered here. Age classes each carry their own count and their own reserve. A count with a mean reserve cannot tell a provisioned cohort from one where half the members hold nothing, and nothing downstream can recover what that discarded. Within a class members are indistinguishable; that is the declared resolution and a stated loss. No accessor returns a mean, because an average that is easy to reach becomes the authority. Totals are summed from the classes rather than stored beside them, following the archive's rule that a second tally is a thing that can disagree. Lineage is not a field at all: ancestry is resolved from the genome through the archive, so a cohort cannot contradict it or invent parentage from membership. Residuals are integer numerators strictly below one member and are never added to a count. Ownership is enforced rather than described. One record per habitat and genome makes no-inventory-spendable-in-two-tiers unrepresentable instead of forbidden, and replacement is refused under a different worker or an older epoch. That is a single-process invariant; fencing between workers is M7 and M10. The approximation bound is carried and never computed, since carrying a bound is not meeting one. All nine guards were confirmed load-bearing by removing each and rerunning. An earlier sweep reporting none of them load-bearing was wrong and discarded: it wrote to /tmp, which Git Bash and PowerShell resolve differently, so nothing was ever mutated. Release 34/34, analysis, ASan 34/34 and clang-format all ran and passed here. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
#include "evo/ecology/cohort.hpp"
|
||||
|
||||
#include <algorithm>
|
||||
#include <limits>
|
||||
#include <utility>
|
||||
|
||||
namespace evo::ecology {
|
||||
namespace {
|
||||
constexpr auto maximum = std::numeric_limits<std::uint64_t>::max();
|
||||
|
||||
[[nodiscard]] bool add(std::uint64_t& total, std::uint64_t amount) noexcept {
|
||||
if (amount > maximum - total) {
|
||||
return false;
|
||||
}
|
||||
total += amount;
|
||||
return true;
|
||||
}
|
||||
// Summed per channel: a total mass is not an additional inventory, so the
|
||||
// channels are added separately and any one of them may be what overflows.
|
||||
[[nodiscard]] bool add(accounting::Inventory& total, const accounting::Inventory& amount) noexcept {
|
||||
if (!add(total.energy.value, amount.energy.value)) {
|
||||
return false;
|
||||
}
|
||||
for (std::size_t channel = 0; channel < total.matter.size(); ++channel) {
|
||||
if (!add(total.matter[channel].value, amount.matter[channel].value)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
[[nodiscard]] bool empty(const accounting::Inventory& inventory) noexcept {
|
||||
return inventory == accounting::Inventory{};
|
||||
}
|
||||
// What the registry is keyed by, which is also the uniqueness it enforces: one
|
||||
// record per habitat and genome.
|
||||
struct Key {
|
||||
HabitatId habitat;
|
||||
std::uint64_t genome;
|
||||
auto operator<=>(const Key&) const = default;
|
||||
};
|
||||
[[nodiscard]] Key key_of(const Cohort& cohort) noexcept {
|
||||
return {cohort.habitat(), cohort.genome()};
|
||||
}
|
||||
} // namespace
|
||||
|
||||
std::expected<Cohort, CohortError> Cohort::create(CohortDraft draft) {
|
||||
if (draft.representation_version != cohort_representation_version) {
|
||||
return std::unexpected{CohortError::unsupported_version};
|
||||
}
|
||||
// Every identity a cohort names has to name something. A zero owner or epoch
|
||||
// would be a record nobody is responsible for integrating.
|
||||
if (draft.habitat.value == 0 || draft.genome == 0 || draft.owner.worker == 0 ||
|
||||
draft.owner.epoch == 0 || draft.conversion == 0) {
|
||||
return std::unexpected{CohortError::invalid_identity};
|
||||
}
|
||||
if (draft.classes.empty()) {
|
||||
return std::unexpected{CohortError::no_age_classes};
|
||||
}
|
||||
if (draft.classes.size() > max_age_classes) {
|
||||
return std::unexpected{CohortError::overflow};
|
||||
}
|
||||
std::uint64_t count = 0;
|
||||
accounting::Inventory reserve{};
|
||||
std::uint64_t residual = 0;
|
||||
for (std::size_t index = 0; index < draft.classes.size(); ++index) {
|
||||
const auto& age = draft.classes[index];
|
||||
// Ascending, non-overlapping and non-empty. An empty or overlapping range
|
||||
// would make a member's class ambiguous or impossible.
|
||||
if (age.age_high <= age.age_low) {
|
||||
return std::unexpected{CohortError::unordered_age_classes};
|
||||
}
|
||||
if (index > 0 && age.age_low < draft.classes[index - 1].age_high) {
|
||||
return std::unexpected{CohortError::unordered_age_classes};
|
||||
}
|
||||
// A residual is a fraction of one member. At a whole member it is one.
|
||||
if (age.residual >= residual_scale) {
|
||||
return std::unexpected{CohortError::residual_out_of_range};
|
||||
}
|
||||
// A class with neither members nor a fraction owed is not a class; it is
|
||||
// a boundary someone declared and left empty.
|
||||
if (age.count == 0 && age.residual == 0) {
|
||||
return std::unexpected{CohortError::empty_age_class};
|
||||
}
|
||||
// Reserve belongs to members. A class with no members holding inventory
|
||||
// would be biomass owned by nobody, which no later transfer could settle.
|
||||
if (age.count == 0 && !empty(age.reserve)) {
|
||||
return std::unexpected{CohortError::unowned_reserve};
|
||||
}
|
||||
if (!add(count, age.count) || !add(reserve, age.reserve) || !add(residual, age.residual)) {
|
||||
return std::unexpected{CohortError::overflow};
|
||||
}
|
||||
}
|
||||
Cohort cohort;
|
||||
cohort.version_ = draft.representation_version;
|
||||
cohort.habitat_ = draft.habitat;
|
||||
cohort.genome_ = draft.genome;
|
||||
cohort.owner_ = draft.owner;
|
||||
cohort.last_tick_ = draft.last_tick;
|
||||
cohort.conversion_ = draft.conversion;
|
||||
cohort.classes_ = std::move(draft.classes);
|
||||
cohort.bound_ = draft.bound;
|
||||
return cohort;
|
||||
}
|
||||
|
||||
std::uint64_t Cohort::count() const noexcept {
|
||||
std::uint64_t total = 0;
|
||||
for (const auto& age : classes_) {
|
||||
// Construction proved this cannot overflow, so the sum is exact here.
|
||||
total += age.count;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
accounting::Inventory Cohort::reserve() const noexcept {
|
||||
accounting::Inventory total{};
|
||||
for (const auto& age : classes_) {
|
||||
total.energy.value += age.reserve.energy.value;
|
||||
for (std::size_t channel = 0; channel < total.matter.size(); ++channel) {
|
||||
total.matter[channel].value += age.reserve.matter[channel].value;
|
||||
}
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
std::uint64_t Cohort::residual() const noexcept {
|
||||
std::uint64_t total = 0;
|
||||
for (const auto& age : classes_) {
|
||||
total += age.residual;
|
||||
}
|
||||
return total;
|
||||
}
|
||||
|
||||
std::expected<void, CohortError> CohortRegistry::install(const Cohort& cohort) {
|
||||
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) {
|
||||
return std::unexpected{CohortError::duplicate_cohort};
|
||||
}
|
||||
if (cohorts_.size() >= max_cohorts) {
|
||||
return std::unexpected{CohortError::registry_full};
|
||||
}
|
||||
cohorts_.insert(at, cohort);
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<void, CohortError> CohortRegistry::replace(const Cohort& cohort) {
|
||||
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
|
||||
// either is not a newer view of this cohort; it names one that is not here.
|
||||
if (at == cohorts_.end() || key_of(*at) != wanted) {
|
||||
return std::unexpected{CohortError::unknown_cohort};
|
||||
}
|
||||
if (cohort.owner().worker != at->owner().worker) {
|
||||
return std::unexpected{CohortError::wrong_owner};
|
||||
}
|
||||
// Equal is accepted: an owner rewriting its own epoch is the ordinary case.
|
||||
// Older is refused, so a writer holding a stale view cannot install it.
|
||||
if (cohort.owner().epoch < at->owner().epoch) {
|
||||
return std::unexpected{CohortError::stale_epoch};
|
||||
}
|
||||
*at = cohort;
|
||||
return {};
|
||||
}
|
||||
|
||||
std::expected<Cohort, CohortError> CohortRegistry::find(HabitatId habitat,
|
||||
std::uint64_t genome) const {
|
||||
const Key wanted{habitat, genome};
|
||||
const auto at = std::ranges::lower_bound(cohorts_, wanted, {}, key_of);
|
||||
if (at == cohorts_.end() || key_of(*at) != wanted) {
|
||||
return std::unexpected{CohortError::unknown_cohort};
|
||||
}
|
||||
return *at;
|
||||
}
|
||||
} // namespace evo::ecology
|
||||
Reference in New Issue
Block a user