M12.2: select and qualify exact cellular local cohorts
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m19s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (tsan) (push) Canceled after 6m24s
Foundation / checks (network) (push) Canceled after 6m33s
Foundation / checks (postgres) (push) Canceled after 6m34s
Foundation / checks (checks) (push) Canceled after 6m44s
Foundation / checks (client) (push) Canceled after 6m44s

This commit is contained in:
dingo
2026-10-17 12:04:39 -04:00
parent bee57e5829
commit 2b074b9ae1
17 changed files with 762 additions and 149 deletions
@@ -0,0 +1,92 @@
#include "evo/server/lifecycle/cellular_population.hpp"
#include "evo/server/lifecycle/wildlife_controller.hpp"
#include <algorithm>
#include <utility>
namespace evo::server {
CellularPopulation::CellularPopulation(std::unique_ptr<CellularLives> state)
: state_(std::move(state)) {}
std::expected<std::unique_ptr<CellularPopulation>, CellularPopulationError>
CellularPopulation::create(std::unique_ptr<CellularLives> state) {
if (!state || state->rules_version() != cellular_lives_foraging_rules)
return std::unexpected(CellularPopulationError::unsupported);
return std::unique_ptr<CellularPopulation>(new CellularPopulation(std::move(state)));
}
const CellularLives& CellularPopulation::state() const noexcept {
return *state_;
}
std::span<const CellularCohortMember> CellularPopulation::members() const noexcept {
return members_;
}
bool CellularPopulation::in_cohort(std::uint64_t life) const noexcept {
return std::ranges::binary_search(members_, life, {}, &CellularCohortMember::life);
}
std::expected<std::size_t, CellularPopulationError>
CellularPopulation::collapse(std::uint64_t genome) {
auto candidate = members_;
std::size_t converted = 0;
bool found = false;
for (const auto& life : state_->lives()) {
if (life.identity.genome != genome || life.account != LifeAccount{})
continue;
found = true;
if (life.presence != CellularPresence::active)
return std::unexpected(CellularPopulationError::ineligible);
if (!in_cohort(life.identity.life)) {
candidate.push_back({life.identity.life, genome});
++converted;
}
}
if (!found)
return std::unexpected(CellularPopulationError::absent);
std::ranges::sort(candidate);
members_.swap(candidate);
return converted;
}
std::expected<std::size_t, CellularPopulationError>
CellularPopulation::materialize(std::uint64_t genome) {
const auto removed =
std::erase_if(members_, [genome](const auto& m) { return m.genome == genome; });
if (removed == 0)
return std::unexpected(CellularPopulationError::absent);
return removed;
}
std::expected<CellularTickExecution, CellularLivesError>
CellularPopulation::advance_tick(std::span<const CellularLifeIntent> controlled,
std::span<const CellularMutationSelection> selections) {
auto wildlife = wildlife_intents(*state_, state_->tick() + 1);
if (!wildlife)
return std::unexpected(wildlife.error());
std::vector<CellularLifeIntent> intents;
intents.reserve(state_->lives().size());
std::size_t player = 0, autonomous = 0;
for (const auto& life : state_->lives()) {
if (life.account == LifeAccount{}) {
intents.push_back((*wildlife)[autonomous++]);
} else {
if (player == controlled.size() || controlled[player].life_id != life.identity.life)
return std::unexpected(CellularLivesError::invalid_intents);
intents.push_back(controlled[player++]);
}
}
if (player != controlled.size())
return std::unexpected(CellularLivesError::invalid_intents);
for (const auto& selection : selections) {
if (std::ranges::find(controlled, selection.life_id, &CellularLifeIntent::life_id) ==
controlled.end())
return std::unexpected(CellularLivesError::invalid_intents);
}
auto executed = state_->advance_tick_with_mutations(intents, selections);
if (!executed)
return std::unexpected(executed.error());
std::erase_if(members_, [this](const auto& member) {
return std::ranges::find(state_->lives(), member.life, [](const auto& life) {
return life.identity.life;
}) == state_->lives().end();
});
return executed;
}
} // namespace evo::server