#include "evo/server/lifecycle/cellular_population.hpp" #include "evo/server/lifecycle/cellular_schedule.hpp" #include "fixtures/cellular_ecology.hpp" #include #include #include namespace { using namespace evo; using namespace evo::server; using namespace evo::fixture; using Population = std::unique_ptr; Owner descendants() { auto state = opening(1, true); std::set mutated; for (unsigned step = 0; step < 8192; ++step) { const auto input = inputs(*state, 1, mutated); const auto executed = checked(state->advance_tick_with_mutations(input.lives, input.mutations), "descendant fixture tick"); for (const auto& mutation : executed.mutations) if (mutation.result == CellularMutationResult::applied) mutated.insert(mutation.life_id); } require(std::ranges::any_of(state->lives(), [](const auto& life) { return life.account == LifeAccount{}; }), "fixture has no funded descendant"); return state; } Population restored(std::span bytes, const CellularLives& state) { return checked(CellularPopulation::restore(bytes, state.world(), state.region(), state.frame(), state.tick()), "population restore"); } std::uint64_t wildlife_genome(const CellularPopulation& population) { for (const auto& life : population.state().lives()) if (life.account == LifeAccount{}) return life.identity.genome; throw std::runtime_error("missing wildlife genome"); } void frozen_tick(CellularPopulation& population) { std::vector controlled; for (const auto& life : population.state().lives()) if (life.account != LifeAccount{}) { CellularLifeIntent input{life.identity.life, {}}; input.intent.frozen = true; controlled.push_back(input); } checked(population.advance_tick(controlled), "offline population tick"); } void put(std::vector& bytes, std::size_t at, std::uint64_t value, unsigned width = 8) { for (unsigned i = 0; i < width; ++i) { bytes[at++] = static_cast(value & 255U); value >>= 8U; } } void refresh(std::vector& bytes) { std::uint64_t hash = 14695981039346656037ULL; for (const auto byte : std::span{bytes}.first(bytes.size() - 8)) hash = (hash ^ byte) * 1099511628211ULL; put(bytes, bytes.size() - 8, hash); } void conversions_and_codec() { auto population = checked(CellularPopulation::create(descendants()), "population create"); const auto genome = wildlife_genome(*population); const auto before = population->snapshot(); const auto biology = population->state().snapshot(); auto pending = checked(population->conversion_candidate(genome, true), "collapse candidate"); require(population->snapshot() == before && population->members().empty(), "planning consumed predecessor"); require(pending->state().snapshot() == biology && !pending->members().empty() && pending->conversion_sequence() == 1, "candidate changed biology"); // Recovery selects a trusted old/new complete checkpoint; it never rerolls members. require(restored(before, population->state())->snapshot() == before, "old checkpoint recovery"); auto committed = restored(pending->snapshot(), pending->state()); require(committed->snapshot() == pending->snapshot(), "committed sample recovery"); auto active = checked(committed->conversion_candidate(genome, false), "materialize candidate"); require(active->state().snapshot() == biology && active->members().empty() && active->conversion_sequence() == 2, "materialization reset biology"); require(checked(committed->collapse(genome), "idempotent collapse") == 0 && committed->conversion_sequence() == 1, "duplicate conversion advanced sequence"); for (unsigned repeat = 0; repeat < 100; ++repeat) { checked(committed->materialize(genome), "oscillation materialize"); checked(committed->collapse(genome), "oscillation collapse"); committed = restored(committed->snapshot(), committed->state()); require(committed->state().snapshot() == biology, "conversion healed or duplicated biology"); } const auto valid = committed->snapshot(); const auto& state = committed->state(); const auto refuses = [&](const auto& bytes) { require(!CellularPopulation::restore(bytes, state.world(), state.region(), state.frame(), state.tick()), "malformed population admitted"); }; for (std::size_t size = 0; size < valid.size(); size += 97) refuses(std::span{valid}.first(size)); auto changed = valid; changed.back() ^= 1; refuses(changed); changed = valid; changed.push_back(0); refuses(changed); for (const auto offset : {8U, 12U, 16U, 20U, 32U, 40U, 48U, 56U, 64U, 72U, 80U, 88U}) { changed = valid; put(changed, offset, offset == 12 ? 2 : 0, offset < 24 ? 4 : 8); refresh(changed); refuses(changed); } changed = valid; put(changed, 80, 100); // An actual controlled life cannot be reclassified as wildlife. refresh(changed); refuses(changed); changed = valid; put(changed, 24, std::numeric_limits::max()); refresh(changed); auto exhausted = restored(changed, state); const auto exhausted_bytes = exhausted->snapshot(); require(!exhausted->materialize(genome) && exhausted->snapshot() == exhausted_bytes, "exhausted conversion sequence mutated state"); require(!CellularPopulation::create(restore(state), {true, 1800, 1200}), "invalid hysteresis admitted"); } void automatic_and_restart() { auto initial = descendants(); auto policy = checked(CellularPopulation::create(restore(*initial), {true, 1200, 1800}), "automatic owner"); auto exact = checked(CellularPopulation::create(restore(*initial)), "individual control"); const auto genome = wildlife_genome(*policy); checked(policy->collapse(genome), "force near cohort"); frozen_tick(*policy); frozen_tick(*exact); require(policy->members().empty(), "near controlled observer failed to materialize"); std::uint64_t cohort_ticks = 0; for (unsigned tick = 0; tick < 512; ++tick) { frozen_tick(*policy); frozen_tick(*exact); require(policy->state().snapshot() == exact->state().snapshot(), "automatic policy changed biology"); cohort_ticks += policy->members().size(); if (tick % 16 == 0) { const auto bytes = policy->snapshot(); policy = restored(bytes, policy->state()); require(policy->snapshot() == bytes && policy->policy().automatic, "restart lost automatic policy"); } } require(cohort_ticks > 0, "suspended observers prevented unattended cohort collapse"); require(std::ranges::all_of(policy->state().lives(), [](const auto& life) { return life.account == LifeAccount{} || life.presence == CellularPresence::suspended; }) && cellular_unattended_tick_needed(policy->state()), "funded wildlife lost scheduling demand after players suspended"); } void write(const char* path, const CellularPopulation& population) { const auto bytes = population.snapshot(); std::ofstream stream(path, std::ios::binary | std::ios::trunc); stream.write(reinterpret_cast(bytes.data()), static_cast(bytes.size())); stream.close(); require(!stream.fail(), "test checkpoint write failed"); } void checkpoint(const char* path, const char* expected) { auto population = checked(CellularPopulation::create(descendants(), {true, 1200, 1800}), "process checkpoint owner"); for (unsigned tick = 0; tick < 250; ++tick) frozen_tick(*population); require(!population->members().empty(), "process checkpoint has no cohort"); write(path, *population); for (unsigned tick = 0; tick < 128; ++tick) frozen_tick(*population); write(expected, *population); } void resume(const char* path, const char* output) { std::ifstream stream(path, std::ios::binary | std::ios::ate); require(stream.good() && stream.tellg() > 0 && stream.tellg() <= static_cast(max_cellular_population_bytes), "test checkpoint read bound"); const auto size = static_cast(stream.tellg()); stream.seekg(0); std::vector bytes(size); stream.read(reinterpret_cast(bytes.data()), static_cast(size)); require(stream.good(), "test checkpoint read failed"); auto population = checked(CellularPopulation::restore(bytes, 201, 1, 1, 8443), "fresh process restore"); for (unsigned tick = 0; tick < 128; ++tick) frozen_tick(*population); write(output, *population); } } // namespace int main(int argc, char** argv) try { if (argc == 4 && std::string_view{argv[1]} == "--checkpoint") { checkpoint(argv[2], argv[3]); return 0; } if (argc == 4 && std::string_view{argv[1]} == "--resume") { resume(argv[2], argv[3]); return 0; } if (argc != 1) return 2; conversions_and_codec(); automatic_and_restart(); return 0; } catch (const std::exception& error) { std::fprintf(stderr, "%s\n", error.what()); return 1; }