#include "evo/server/lifecycle/primitive_lives.hpp" #include #include #include #include namespace evo::server { struct PrimitiveLives::State { NurseryConfig config; accounting::Ledger ledger; biology::LineageArchive archive; std::vector lives; }; PrimitiveLives::PrimitiveLives(std::unique_ptr state) : state_{std::move(state)} {} PrimitiveLives::~PrimitiveLives() = default; std::expected, LifeError> PrimitiveLives::create(NurseryConfig config) { if (config.world == 0 || config.region == 0 || config.frame == 0 || config.source == 0 || config.sites.empty() || config.sites.size() > max_primitive_lives || !std::ranges::is_sorted(config.sites)) return std::unexpected{LifeError::invalid}; const auto founder = biology::primitive_founder(1, 1); if (!founder) return std::unexpected{LifeError::biology}; const auto phenotype = biology::derive_phenotype(*founder, {1000, 0}, {}); if (!phenotype) return std::unexpected{LifeError::biology}; for (std::size_t i = 0; i < config.sites.size(); ++i) { const auto site = config.sites[i]; if (site.x_um < -spawn_coordinate_limit_um || site.x_um > spawn_coordinate_limit_um || site.y_um < -spawn_coordinate_limit_um || site.y_um > spawn_coordinate_limit_um) return std::unexpected{LifeError::invalid}; for (std::size_t j = 0; j < i; ++j) { const auto dx = std::abs(std::int64_t{site.x_um} - config.sites[j].x_um); const auto dy = std::abs(std::int64_t{site.y_um} - config.sites[j].y_um); if (dx < phenotype->extent && dy < phenotype->extent) return std::unexpected{LifeError::occupied}; } } std::vector opening(2 + 2 * config.sites.size()); opening[0] = config.funding; // Source 1, heat 2; body/reserve pairs follow. auto ledger = accounting::Ledger::create(opening); if (!ledger) return std::unexpected{LifeError::overflow}; auto state = std::make_unique(State{std::move(config), std::move(*ledger), {}, {}}); return std::unique_ptr{new PrimitiveLives{std::move(state)}}; } std::expected PrimitiveLives::spawn(LifeRequest request, LifeAllocation allocation) { if (request.action == 0 || std::ranges::all_of(request.account, [](auto b) { return b == 0; })) return std::unexpected{LifeError::invalid}; for (const auto& life : state_->lives) { if (life.request == request) return life; if (life.request.account == request.account) return std::unexpected{LifeError::existing_life}; } if (allocation.life == 0 || allocation.organism == 0 || allocation.genome == 0 || allocation.event == 0) return std::unexpected{LifeError::invalid}; for (const auto& life : state_->lives) { const auto& old = life.identity; if (old.life == allocation.life || old.organism == allocation.organism || old.genome == allocation.genome || old.event == allocation.event) return std::unexpected{LifeError::identity_conflict}; } const auto slot = state_->lives.size(); if (slot == state_->config.sites.size()) return std::unexpected{LifeError::capacity}; const auto endowed = biology::primitive_endowment(); const auto genome = biology::primitive_founder(allocation.genome, allocation.event); if (!endowed || !genome) return std::unexpected{LifeError::biology}; const auto body = accounting::ReservoirId{static_cast(3 + 2 * slot)}; const auto reserve = accounting::ReservoirId{body.value + 1}; const PrimitiveLife life{ request, allocation, state_->config.source, state_->config.sites[slot], endowed->growth, body, reserve}; // Detached candidate: all fallible work, including allocations and archive // admission, precedes the single noexcept pointer swap. auto next = std::make_unique(*state_); const std::array legs{accounting::Transfer{{1}, body, endowed->body}, accounting::Transfer{{1}, reserve, endowed->reserve}, accounting::Transfer{{1}, {2}, {endowed->construction, {}}}}; const auto paid = next->ledger.transfer(slot + 1, legs); if (!paid) return std::unexpected{paid.error() == accounting::LedgerError::insufficient_inventory ? LifeError::insufficient_funding : LifeError::overflow}; if (!next->archive.admit(*genome) || !next->archive.record_birth(allocation.organism, allocation.genome, allocation.born)) return std::unexpected{LifeError::archive}; next->lives.push_back(life); state_.swap(next); return life; } const NurseryConfig& PrimitiveLives::config() const noexcept { return state_->config; } std::span PrimitiveLives::lives() const noexcept { return state_->lives; } std::span PrimitiveLives::balances() const noexcept { return state_->ledger.balances(); } accounting::Inventory PrimitiveLives::totals() const noexcept { return state_->ledger.conserved_totals(); } const biology::LineageArchive& PrimitiveLives::archive() const noexcept { return state_->archive; } } // namespace evo::server