Two defects from review of 1c8b292.
Replacement checked the ownership epoch and nothing that ordered two writes made
inside one, so an owner could replay its own earlier record: a cohort installed
at tick 1,000 holding 50 picojoules accepted its own tick-900 record holding 100,
restoring inventory the installed one had already spent. A cohort now carries a
revision that strictly increases within an epoch, and a repeated revision is
refused as well as an older one, because a write has to be strictly later rather
than merely not earlier. A newer epoch may restart the series, since re-acquiring
ownership begins a new one, but the interval boundary is checked independently of
both, so neither advancing a revision nor re-acquiring ownership rewinds
simulated time. A later revision at the same boundary stays legitimate: that is a
correction, not a rewind.
The implicit move emptied a cohort's age classes while keeping its habitat,
genome and owner, so a moved-from record was exactly the invalid cohort create
refuses to build, and the registry accepted through a move what it rejects
through construction. The type is copy-only now, so a move decays to a copy and
the source stays whole. ADR-0030 made the genome copy-only for this same reason
in M3.1; the precedent existed and was not applied when this type was written.
Both fixes were confirmed load-bearing by reverting each alone. Removing either
ordering check fails the new regression. Removing the copy declarations fails to
compile, and not only through the static assertion: with that also removed, MSVC
proves the code after the class-count check unreachable, having established that
a moved-from vector is empty and the check always throws. That is a compile-time
proof rather than an execution, so the runtime path was checked separately under
ASan, where the same mutation exits 1.
Release 34/34, analysis, ASan 34/34 and clang-format all ran and passed here.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
196 lines
7.9 KiB
C++
196 lines
7.9 KiB
C++
#pragma once
|
|
|
|
#include "evo/accounting/ledger.hpp"
|
|
#include "evo/core/types.hpp"
|
|
|
|
#include <expected>
|
|
#include <span>
|
|
#include <vector>
|
|
|
|
namespace evo::ecology {
|
|
// Where a cohort lives. M4.1 defines no geometry, extent or neighbourhood: this
|
|
// is an identity a later milestone gives dimensions to.
|
|
struct HabitatId {
|
|
std::uint64_t value{};
|
|
auto operator<=>(const HabitatId&) const = default;
|
|
};
|
|
// Who integrates this cohort, and for which epoch. ECOLOGY_MODEL locks that no
|
|
// inventory is spendable in two tiers, so a cohort has exactly one owner and a
|
|
// writer holding an older epoch is refused rather than merged.
|
|
struct Owner {
|
|
std::uint64_t worker{};
|
|
std::uint64_t epoch{};
|
|
auto operator<=>(const Owner&) const = default;
|
|
};
|
|
|
|
// Fractional members are carried as a numerator over this scale. Integer only:
|
|
// no quantity in this project is a float. A residual is strictly less than one
|
|
// member, because at one member it is a member.
|
|
inline constexpr std::uint64_t residual_scale = 1'000'000;
|
|
|
|
// One age class, holding its own count and its own reserve. The record keeps a
|
|
// distribution rather than a mean, because a mean cannot say whether anyone in
|
|
// the class would survive. Within a class members are indistinguishable; that is
|
|
// the declared resolution, and a caller needing finer structure declares
|
|
// narrower classes rather than dividing a total afterwards.
|
|
struct AgeClass {
|
|
std::uint64_t age_low{}; // ticks, inclusive
|
|
std::uint64_t age_high{}; // ticks, exclusive; must exceed age_low
|
|
std::uint64_t count{};
|
|
// Held by this class in total, not per member. Dividing it would produce the
|
|
// average this record exists to avoid.
|
|
accounting::Inventory reserve{};
|
|
// Owed to this class and not yet present, in units of residual_scale. It is
|
|
// not a count and is never added to one.
|
|
std::uint64_t residual{};
|
|
auto operator<=>(const AgeClass&) const = default;
|
|
};
|
|
|
|
// How far this representation may differ from simulating the same members
|
|
// individually. Declared by whoever built the cohort and never computed here:
|
|
// M4.5 measures resolution equivalence, and a bound this type derived for itself
|
|
// would be a claim it has no basis for. Carrying a bound is not meeting one.
|
|
struct ApproximationBound {
|
|
std::uint64_t count{};
|
|
accounting::Inventory inventory{};
|
|
auto operator<=>(const ApproximationBound&) const = default;
|
|
};
|
|
|
|
struct CohortDraft {
|
|
std::uint32_t representation_version{};
|
|
HabitatId habitat{};
|
|
// The genome every member carries. Distinct genomes are never averaged into
|
|
// one, so a cohort is per genome. Lineage is deliberately absent: it is the
|
|
// archive's answer, resolved from this identity.
|
|
std::uint64_t genome{};
|
|
Owner owner{};
|
|
// The last interval boundary this cohort was integrated to. It never runs
|
|
// backwards: a record naming an earlier boundary is an older view.
|
|
Tick last_tick{};
|
|
// Strictly increases with every replacement within one owner epoch, so two
|
|
// writes that share a boundary are still ordered. An epoch that is itself
|
|
// newer may restart it, because re-acquiring ownership begins a new series.
|
|
std::uint64_t revision{};
|
|
// The conversion that produced this record, kept as provenance.
|
|
std::uint64_t conversion{};
|
|
std::vector<AgeClass> classes;
|
|
ApproximationBound bound{};
|
|
};
|
|
|
|
enum class CohortError : std::uint8_t {
|
|
unsupported_version,
|
|
invalid_identity,
|
|
no_age_classes,
|
|
unordered_age_classes,
|
|
empty_age_class,
|
|
unowned_reserve,
|
|
residual_out_of_range,
|
|
overflow,
|
|
duplicate_cohort,
|
|
unknown_cohort,
|
|
wrong_owner,
|
|
stale_epoch,
|
|
stale_revision,
|
|
backward_tick,
|
|
registry_full
|
|
};
|
|
|
|
inline constexpr std::uint32_t cohort_representation_version = 1;
|
|
inline constexpr std::size_t max_age_classes = 64;
|
|
inline constexpr std::size_t max_cohorts = 4'096;
|
|
|
|
// A validated tier 2 record. Every invariant holds for as long as the value does:
|
|
// there is no field-by-field assembly and no mutation after construction.
|
|
class Cohort {
|
|
public:
|
|
[[nodiscard]] static std::expected<Cohort, CohortError> create(CohortDraft draft);
|
|
// Copy-only on purpose. A moved-from record would keep its habitat, genome,
|
|
// owner and totals while losing its age classes, which is precisely the
|
|
// invalid cohort `create` refuses to build; the registry would then accept
|
|
// through a move what it rejects through construction. Declaring the copy
|
|
// operations suppresses the implicit moves, so a move decays to a copy and
|
|
// the source stays valid. A cohort is at most 64 classes, so that is cheap.
|
|
Cohort(const Cohort&) = default;
|
|
Cohort& operator=(const Cohort&) = default;
|
|
|
|
[[nodiscard]] std::uint32_t representation_version() const noexcept {
|
|
return version_;
|
|
}
|
|
[[nodiscard]] HabitatId habitat() const noexcept {
|
|
return habitat_;
|
|
}
|
|
[[nodiscard]] std::uint64_t genome() const noexcept {
|
|
return genome_;
|
|
}
|
|
[[nodiscard]] Owner owner() const noexcept {
|
|
return owner_;
|
|
}
|
|
[[nodiscard]] Tick last_tick() const noexcept {
|
|
return last_tick_;
|
|
}
|
|
[[nodiscard]] std::uint64_t revision() const noexcept {
|
|
return revision_;
|
|
}
|
|
[[nodiscard]] std::uint64_t conversion() const noexcept {
|
|
return conversion_;
|
|
}
|
|
[[nodiscard]] std::span<const AgeClass> classes() const noexcept {
|
|
return classes_;
|
|
}
|
|
[[nodiscard]] ApproximationBound bound() const noexcept {
|
|
return bound_;
|
|
}
|
|
|
|
// Derived by summing the classes rather than stored beside them, so no two
|
|
// fields of a cohort can disagree. Both reject overflow at construction, so
|
|
// these cannot fail afterwards.
|
|
[[nodiscard]] std::uint64_t count() const noexcept;
|
|
[[nodiscard]] accounting::Inventory reserve() const noexcept;
|
|
// Total owed fraction, in units of residual_scale. May exceed one member once
|
|
// summed across classes; the per-class bound is what is invariant.
|
|
[[nodiscard]] std::uint64_t residual() const noexcept;
|
|
// Deliberately no mean age and no per-member reserve. An average that is easy
|
|
// to reach is an average that gets used as the authority.
|
|
|
|
auto operator<=>(const Cohort&) const = default;
|
|
|
|
private:
|
|
Cohort() = default;
|
|
std::uint32_t version_{};
|
|
HabitatId habitat_{};
|
|
std::uint64_t genome_{};
|
|
Owner owner_{};
|
|
Tick last_tick_{};
|
|
std::uint64_t revision_{};
|
|
std::uint64_t conversion_{};
|
|
std::vector<AgeClass> classes_;
|
|
ApproximationBound bound_{};
|
|
};
|
|
|
|
// Holds at most one cohort per habitat and genome. Two records of one genome in
|
|
// one habitat would be two places the same biomass could be spent, which is the
|
|
// invariant ECOLOGY_MODEL locks; here it is unrepresentable rather than
|
|
// forbidden. Nothing is serialized and no cross-worker protocol is implied.
|
|
class CohortRegistry {
|
|
public:
|
|
[[nodiscard]] std::expected<void, CohortError> install(const Cohort& cohort);
|
|
// Refused unless it is a strictly later view of the same record by the same
|
|
// owner: the habitat and genome must match, the epoch must not be older, the
|
|
// revision must advance within an epoch, and the interval boundary must not
|
|
// run backwards. An epoch alone is too coarse, because two writes inside one
|
|
// epoch would otherwise be interchangeable and replaying the earlier one
|
|
// would restore inventory the later one had already spent.
|
|
[[nodiscard]] std::expected<void, CohortError> replace(const Cohort& cohort);
|
|
[[nodiscard]] std::expected<Cohort, CohortError> find(HabitatId habitat,
|
|
std::uint64_t genome) const;
|
|
[[nodiscard]] std::size_t size() const noexcept {
|
|
return cohorts_.size();
|
|
}
|
|
|
|
private:
|
|
// Sorted by habitat then genome, so a lookup is a binary search and there is
|
|
// one record of each thing with no index that could disagree with it.
|
|
std::vector<Cohort> cohorts_;
|
|
};
|
|
} // namespace evo::ecology
|