M12.3: persist exact population state and automatic conversion policy
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 3m16s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (client) (push) Canceled after 13m8s
Foundation / checks (checks) (push) Canceled after 13m12s
Foundation / checks (postgres) (push) Canceled after 13m2s
Foundation / checks (network) (push) Canceled after 13m4s
Foundation / checks (tsan) (push) Canceled after 13m9s

This commit is contained in:
dingo
2026-10-17 12:11:10 -04:00
parent 2b074b9ae1
commit 5decaa0326
14 changed files with 624 additions and 15 deletions
+30 -7
View File
@@ -3,17 +3,23 @@
#include "evo/server/lifecycle/wildlife_controller.hpp"
#include <algorithm>
#include <limits>
#include <set>
#include <utility>
namespace evo::server {
CellularPopulation::CellularPopulation(std::unique_ptr<CellularLives> state)
: state_(std::move(state)) {}
CellularPopulation::CellularPopulation(std::unique_ptr<CellularLives> state,
CellularTierPolicy policy)
: state_(std::move(state)), policy_(policy) {}
std::expected<std::unique_ptr<CellularPopulation>, CellularPopulationError>
CellularPopulation::create(std::unique_ptr<CellularLives> state) {
if (!state || state->rules_version() != cellular_lives_foraging_rules)
CellularPopulation::create(std::unique_ptr<CellularLives> state, CellularTierPolicy policy) {
if (!state || state->rules_version() != cellular_lives_foraging_rules ||
policy.materialize_radius_um == 0 ||
policy.collapse_radius_um <= policy.materialize_radius_um ||
policy.collapse_radius_um > biology::cellular_motion_axis_limit_um)
return std::unexpected(CellularPopulationError::unsupported);
return std::unique_ptr<CellularPopulation>(new CellularPopulation(std::move(state)));
return std::unique_ptr<CellularPopulation>(new CellularPopulation(std::move(state), policy));
}
const CellularLives& CellularPopulation::state() const noexcept {
return *state_;
@@ -24,6 +30,12 @@ std::span<const CellularCohortMember> CellularPopulation::members() const noexce
bool CellularPopulation::in_cohort(std::uint64_t life) const noexcept {
return std::ranges::binary_search(members_, life, {}, &CellularCohortMember::life);
}
CellularTierPolicy CellularPopulation::policy() const noexcept {
return policy_;
}
std::uint64_t CellularPopulation::conversion_sequence() const noexcept {
return conversion_sequence_;
}
std::expected<std::size_t, CellularPopulationError>
CellularPopulation::collapse(std::uint64_t genome) {
auto candidate = members_;
@@ -42,21 +54,30 @@ CellularPopulation::collapse(std::uint64_t genome) {
}
if (!found)
return std::unexpected(CellularPopulationError::absent);
if (converted > 0 && conversion_sequence_ == std::numeric_limits<std::uint64_t>::max())
return std::unexpected(CellularPopulationError::overflow);
std::ranges::sort(candidate);
members_.swap(candidate);
if (converted > 0)
++conversion_sequence_;
return converted;
}
std::expected<std::size_t, CellularPopulationError>
CellularPopulation::materialize(std::uint64_t genome) {
if (std::ranges::none_of(members_, [genome](const auto& m) { return m.genome == genome; }))
return std::unexpected(CellularPopulationError::absent);
if (conversion_sequence_ == std::numeric_limits<std::uint64_t>::max())
return std::unexpected(CellularPopulationError::overflow);
const auto removed =
std::erase_if(members_, [genome](const auto& m) { return m.genome == genome; });
if (removed == 0)
return std::unexpected(CellularPopulationError::absent);
++conversion_sequence_;
return removed;
}
std::expected<CellularTickExecution, CellularLivesError>
CellularPopulation::advance_tick(std::span<const CellularLifeIntent> controlled,
std::span<const CellularMutationSelection> selections) {
if (policy_.automatic && conversion_sequence_ == std::numeric_limits<std::uint64_t>::max())
return std::unexpected(CellularLivesError::overflow);
auto wildlife = wildlife_intents(*state_, state_->tick() + 1);
if (!wildlife)
return std::unexpected(wildlife.error());
@@ -87,6 +108,8 @@ CellularPopulation::advance_tick(std::span<const CellularLifeIntent> controlled,
return life.identity.life;
}) == state_->lives().end();
});
if (policy_.automatic)
apply_policy();
return executed;
}
} // namespace evo::server
@@ -0,0 +1,133 @@
#include "evo/server/lifecycle/cellular_population.hpp"
#include <algorithm>
#include <utility>
namespace evo::server {
namespace {
constexpr std::uint64_t magic = 0x31504F504F564541ULL; // "AEVOPOP1"
std::uint64_t checksum(std::span<const std::uint8_t> bytes) {
std::uint64_t hash = 14695981039346656037ULL;
for (const auto byte : bytes)
hash = (hash ^ byte) * 1099511628211ULL;
return hash; // Corruption detection; trusted publication must also bind SHA-256.
}
void put(std::vector<std::uint8_t>& bytes, std::uint64_t value, unsigned width) {
for (unsigned i = 0; i < width; ++i) {
bytes.push_back(static_cast<std::uint8_t>(value & 255U));
value >>= 8U;
}
}
class Reader {
public:
explicit Reader(std::span<const std::uint8_t> bytes) : bytes_(bytes) {}
std::uint64_t get(unsigned width) {
std::uint64_t value = 0;
for (unsigned i = 0; i < width; ++i)
value |= std::uint64_t{bytes_[at_++]} << (8U * i);
return value;
}
private:
std::span<const std::uint8_t> bytes_;
std::size_t at_{};
};
} // namespace
std::vector<std::uint8_t> CellularPopulation::snapshot() const {
const auto biology = state_->snapshot();
std::vector<std::uint8_t> bytes;
bytes.reserve(cellular_population_header_bytes + members_.size() * 16 + biology.size() + 8);
put(bytes, magic, 8);
put(bytes, 1, 4);
put(bytes, policy_.automatic ? 1 : 0, 4);
put(bytes, policy_.materialize_radius_um, 4);
put(bytes, policy_.collapse_radius_um, 4);
for (const auto value :
{conversion_sequence_, state_->world(), state_->region(), state_->frame(), state_->tick(),
static_cast<std::uint64_t>(biology.size()), static_cast<std::uint64_t>(members_.size())})
put(bytes, value, 8);
for (const auto& member : members_) {
put(bytes, member.life, 8);
put(bytes, member.genome, 8);
}
bytes.insert(bytes.end(), biology.begin(), biology.end());
put(bytes, checksum(bytes), 8);
return bytes;
}
std::expected<std::unique_ptr<CellularPopulation>, CellularPopulationError>
CellularPopulation::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
std::uint64_t region, std::uint64_t frame, std::uint64_t tick) {
if (bytes.size() < cellular_population_header_bytes + 8 ||
bytes.size() > max_cellular_population_bytes)
return std::unexpected(CellularPopulationError::format);
Reader tail(bytes.last(8));
if (tail.get(8) != checksum(bytes.first(bytes.size() - 8)))
return std::unexpected(CellularPopulationError::checksum);
Reader reader(bytes);
if (reader.get(8) != magic)
return std::unexpected(CellularPopulationError::format);
if (reader.get(4) != 1)
return std::unexpected(CellularPopulationError::unsupported);
const auto flags = reader.get(4);
if (flags > 1)
return std::unexpected(CellularPopulationError::format);
CellularTierPolicy policy;
policy.automatic = flags == 1;
policy.materialize_radius_um = static_cast<std::uint32_t>(reader.get(4));
policy.collapse_radius_um = static_cast<std::uint32_t>(reader.get(4));
const auto sequence = reader.get(8);
if (reader.get(8) != world || reader.get(8) != region || reader.get(8) != frame ||
reader.get(8) != tick)
return std::unexpected(CellularPopulationError::identity);
const auto biological_bytes = reader.get(8), count = reader.get(8);
if (count > biology::max_cellular_tick_lives || biological_bytes > max_cellular_lives_bytes ||
biological_bytes == 0 ||
bytes.size() != cellular_population_header_bytes + count * 16 + biological_bytes + 8)
return std::unexpected(CellularPopulationError::format);
std::vector<CellularCohortMember> members;
members.reserve(static_cast<std::size_t>(count));
for (std::uint64_t index = 0; index < count; ++index) {
CellularCohortMember member{reader.get(8), reader.get(8)};
if (member.life == 0 || member.genome == 0 ||
(!members.empty() && members.back().life >= member.life))
return std::unexpected(CellularPopulationError::format);
members.push_back(member);
}
const auto offset = cellular_population_header_bytes + static_cast<std::size_t>(count) * 16;
auto biological =
CellularLives::restore(bytes.subspan(offset, static_cast<std::size_t>(biological_bytes)),
world, region, frame, tick);
if (!biological)
return std::unexpected(CellularPopulationError::format);
auto candidate = create(std::move(*biological), policy);
if (!candidate)
return std::unexpected(candidate.error());
for (const auto& member : members) {
const auto life = std::ranges::find((*candidate)->state().lives(), member.life,
[](const auto& cell) { return cell.identity.life; });
if (life == (*candidate)->state().lives().end() || life->account != LifeAccount{} ||
life->presence != CellularPresence::active || life->identity.genome != member.genome)
return std::unexpected(CellularPopulationError::ineligible);
}
(*candidate)->members_ = std::move(members);
(*candidate)->conversion_sequence_ = sequence;
if (!std::ranges::equal((*candidate)->snapshot(), bytes))
return std::unexpected(CellularPopulationError::format);
return candidate;
}
std::expected<std::unique_ptr<CellularPopulation>, CellularPopulationError>
CellularPopulation::conversion_candidate(std::uint64_t genome, bool to_cohort) const {
auto candidate =
restore(snapshot(), state_->world(), state_->region(), state_->frame(), state_->tick());
if (!candidate)
return std::unexpected(candidate.error());
const auto converted =
to_cohort ? (*candidate)->collapse(genome) : (*candidate)->materialize(genome);
if (!converted)
return std::unexpected(converted.error());
return candidate;
}
} // namespace evo::server
@@ -0,0 +1,51 @@
#include "evo/server/lifecycle/cellular_population.hpp"
#include <algorithm>
#include <set>
namespace evo::server {
void CellularPopulation::apply_policy() {
auto candidate = members_;
std::set<std::uint64_t> genomes;
for (const auto& life : state_->lives())
if (life.account == LifeAccount{})
genomes.insert(life.identity.genome);
const auto inner = std::uint64_t{policy_.materialize_radius_um} * policy_.materialize_radius_um;
const auto outer = std::uint64_t{policy_.collapse_radius_um} * policy_.collapse_radius_um;
for (const auto genome : genomes) {
bool near = false, far = true, eligible = true;
for (const auto& life : state_->lives()) {
if (life.account != LifeAccount{} || life.identity.genome != genome)
continue;
eligible = eligible && life.presence == CellularPresence::active;
for (const auto& observer : state_->lives()) {
if (observer.account == LifeAccount{} ||
observer.presence == CellularPresence::suspended)
continue;
const auto dx = life.position.x_um > observer.position.x_um
? life.position.x_um - observer.position.x_um
: observer.position.x_um - life.position.x_um;
const auto dy = life.position.y_um > observer.position.y_um
? life.position.y_um - observer.position.y_um
: observer.position.y_um - life.position.y_um;
const auto distance = dx * dx + dy * dy; // Coordinates bounded to 1e6 um.
near = near || distance <= inner;
far = far && distance > outer;
}
}
if (near || !eligible) {
std::erase_if(candidate, [genome](const auto& m) { return m.genome == genome; });
} else if (far) {
std::erase_if(candidate, [genome](const auto& m) { return m.genome == genome; });
for (const auto& life : state_->lives())
if (life.account == LifeAccount{} && life.identity.genome == genome)
candidate.push_back({life.identity.life, genome});
}
}
std::ranges::sort(candidate);
if (candidate != members_) {
members_.swap(candidate);
++conversion_sequence_;
}
}
} // namespace evo::server