#pragma once #include "evo/server/lifecycle/cellular_lives.hpp" #include #include #include #include #include #include namespace evo::server { struct CellularPopulationBatch; // ADR-0139: exact local cohorts retain individuals, never mean reserves or samples. // The single owned CellularLives store backs BOTH representations, exactly once. struct CellularCohortMember { std::uint64_t life{}, genome{}; auto operator<=>(const CellularCohortMember&) const = default; }; enum class CellularPopulationError : std::uint8_t { unsupported, absent, ineligible, format, checksum, identity, overflow }; struct CellularTierPolicy { bool automatic{false}; std::uint32_t materialize_radius_um{1'200}, collapse_radius_um{1'800}; auto operator<=>(const CellularTierPolicy&) const = default; }; inline constexpr std::size_t cellular_population_header_bytes = 80; inline constexpr std::size_t max_cellular_population_bytes = cellular_population_header_bytes + 16 * biology::max_cellular_tick_lives + max_cellular_lives_bytes + 8; class CellularPopulation { public: [[nodiscard]] static std::expected, CellularPopulationError> create(std::unique_ptr state, CellularTierPolicy policy = {}); CellularPopulation(const CellularPopulation&) = delete; CellularPopulation& operator=(const CellularPopulation&) = delete; // Read-only biology view: it confers no separate spending rights. [[nodiscard]] const CellularLives& state() const noexcept; [[nodiscard]] std::span members() const noexcept; [[nodiscard]] bool in_cohort(std::uint64_t life) const noexcept; [[nodiscard]] CellularTierPolicy policy() const noexcept; [[nodiscard]] std::uint64_t conversion_sequence() const noexcept; [[nodiscard]] std::uint32_t format_version() const noexcept; // Explicit no-tick envelope-1 -> envelope-2 upgrade, retaining all cohort tags. [[nodiscard]] std::expected, CellularPopulationError> migration_upgrade_candidate() const; // ADR-0157: preserve exact membership while upgrading nested biology to rules 9. [[nodiscard]] std::expected, CellularPopulationError> maturation_upgrade_candidate() const; [[nodiscard]] std::vector snapshot() const; [[nodiscard]] static std::expected, CellularPopulationError> restore(std::span bytes, std::uint64_t world, std::uint64_t region, std::uint64_t frame, std::uint64_t tick); // Detached exact sample for publication. Never acknowledge it before a fenced // durable head commits its complete snapshot; recovery restores that sample. [[nodiscard]] std::expected, CellularPopulationError> conversion_candidate(std::uint64_t genome, bool to_cohort) const; // Convert the WHOLE eligible wildlife genome group. No selectable healthy subset, // resampling, rounding, new identity, healing, age reset or inventory transfer. // Controlled lives are never members. Refusal leaves biology and ownership unchanged. [[nodiscard]] std::expected collapse(std::uint64_t genome); [[nodiscard]] std::expected materialize(std::uint64_t genome); // Only controlled-life intentions/selections are supplied by the host, sorted by ID. // All wildlife, including cohorts, uses the unchanged shared controller and kernel. [[nodiscard]] std::expected advance_tick(std::span controlled, std::span selections = {}); private: friend class CellularMigration; friend std::expected, CellularPopulationError> execute_population_batch(const CellularPopulation&, CellularPopulationBatch&, bool); explicit CellularPopulation(std::unique_ptr state, CellularTierPolicy policy); void apply_policy(); std::unique_ptr state_; std::vector members_; // Sorted life IDs, unique ownership tags. CellularTierPolicy policy_; std::uint64_t conversion_sequence_{}; }; } // namespace evo::server