#include "evo/server/lifecycle/cellular_census.hpp" #include "evo/server/lifecycle/cellular_census_set.hpp" #include "fixtures/cellular_ecology.hpp" #include namespace { using namespace evo; using namespace evo::server; using namespace evo::fixture; using Kind = CellularCensusKind; using Scope = CellularCensusScope; std::size_t index(Kind kind) { return static_cast(kind); } Inventory sum(Inventory left, const Inventory& right) { require(right.energy.value <= std::numeric_limits::max() - left.energy.value, "oracle energy overflow"); left.energy.value += right.energy.value; for (std::size_t channel = 0; channel < left.matter.size(); ++channel) { require(right.matter[channel].value <= std::numeric_limits::max() - left.matter[channel].value, "oracle matter overflow"); left.matter[channel].value += right.matter[channel].value; } return left; } CellularCensusTotals direct(const CellularLives& state, std::uint64_t anchor, bool descendants, const CellularPopulation* population = nullptr) { CellularCensusTotals result; for (const auto& life : state.lives()) { const auto ancestry = checked(state.archive().ancestry(life.identity.genome), "oracle ancestry"); if (descendants ? std::ranges::find(ancestry, anchor) == ancestry.end() : life.identity.genome != anchor) continue; std::size_t kind{}; if (life.account == LifeAccount{}) kind = population && population->in_cohort(life.identity.life) ? 1 : 0; else kind = 2 + static_cast(life.presence); auto& stock = result.resident[kind]; ++stock.count; stock.body = sum(stock.body, life.body); stock.reserve = sum(stock.reserve, life.reserve); } return result; } void verify(const CellularCensus& census, const CellularLives& state, const CellularPopulation* population = nullptr) { require(census.boundary() == CellularCensusBoundary{state.world(), state.region(), state.frame(), state.tick(), state.format_version(), state.rules_version(), population ? population->format_version() : 0}, "census lost its local/version/time boundary"); require(census.rows().size() == state.archive().archived(), "census discarded retained genomes"); require(std::ranges::is_sorted(census.rows(), {}, &CellularCensusRow::genome), "census genome order"); for (const auto& row : census.rows()) { const auto genome = checked(state.archive().find(row.genome), "census retained genome"); require(row.schema == genome.schema_version() && row.development == genome.development_version() && std::ranges::equal(row.parents, genome.parents()), "census rewrote genome provenance"); require(checked(census.query(row.genome, Scope::exact_genome), "exact census query") == direct(state, row.genome, false, population) && checked(census.query(row.genome, Scope::descendants), "lineage census query") == direct(state, row.genome, true, population), "census differs from direct current-individual count/inventory"); } } void step(CellularLives& state, bool freeze_first = false) { std::vector intents; for (const auto& life : state.lives()) { CellularLifeIntent input{life.identity.life, {}}; input.intent.frozen = freeze_first && life.identity.life == 100; intents.push_back(input); } checked(state.advance_tick(intents), "census fixture tick"); } void presence_and_detachment() { auto owner = opening(1, true); const auto before = owner->snapshot(); const auto initial = checked(CellularCensus::observe(*owner), "initial census"); verify(initial, *owner); require(owner->snapshot() == before, "observation changed biology"); require(!initial.query(0, Scope::exact_genome) && initial.query(0, Scope::exact_genome).error() == CellularCensusError::unknown_genome && !initial.query(100, Scope::invalid) && initial.query(100, Scope::invalid).error() == CellularCensusError::invalid_scope, "unknown genome/scope became a zero report"); step(*owner, true); auto lingering = checked(CellularCensus::observe(*owner), "lingering census"); verify(lingering, *owner); require(checked(lingering.query(100, Scope::exact_genome), "lingering founder") .resident[index(Kind::lingering_controlled)] .count == 1, "lingering player disappeared from living stock"); for (std::uint64_t ticks = 0; ticks <= cellular_linger_ticks; ++ticks) step(*owner, true); const auto suspended = checked(CellularCensus::observe(*owner), "suspended census"); verify(suspended, *owner); require(checked(suspended.query(100, Scope::exact_genome), "suspended founder") .resident[index(Kind::suspended_controlled)] .count == 1 && checked(initial.query(100, Scope::exact_genome), "detached old founder") .resident[index(Kind::active_controlled)] .count == 1 && initial.boundary().tick < suspended.boundary().tick, "suspension lost viability or rewrote an earlier observation"); const auto recovered = restore(*owner); require( std::ranges::equal(checked(CellularCensus::observe(*recovered), "restored census").rows(), suspended.rows()), "restore changed census"); } void mutation_and_retention() { auto owner = opening(1, true, 1); constexpr std::uint64_t first_successor = 9'000'000'000'000'000'000ULL; // The grandchild ID precedes its parent in numeric order. Sparse IDs and // ancestry depth must not become a numeric scan or a topological assumption. for (const auto successor : {first_successor, first_successor - 1}) { bool applied = false; for (unsigned tick = 0; tick < 8192 && !applied; ++tick) { const auto& life = owner->lives().front(); std::vector choices; if (life.mutation.offer) { const auto& offer = *life.mutation.offer; for (const auto& candidate : offer.candidates) { const auto genome = checked(owner->archive().find(life.identity.genome), "mutation genome"); if (biology::fund_mutation(genome, offer.context, offer, candidate.operator_id, successor, successor, life.reserve)) { choices.push_back({life.identity.life, life.mutation.event_index, life.mutation.offer_tick, successor, candidate.operator_id}); break; } } } const std::array intent{{{100, {}}}}; const auto result = checked(owner->advance_tick_with_mutations(intent, choices), "funded census mutation"); applied = !result.mutations.empty() && result.mutations.front().result == CellularMutationResult::applied; } require(applied, "census fixture never funded a mutation"); } const auto census = checked(CellularCensus::observe(*owner), "mutation census"); verify(census, *owner); require(census.rows().size() == 3 && census.rows().back().genome == first_successor && checked(owner->archive().organism(100), "immutable birth").genome == 100 && checked(census.query(100, Scope::exact_genome), "old exact carriers") == CellularCensusTotals{} && checked(census.query(first_successor, Scope::exact_genome), "zero-carrier middle") == CellularCensusTotals{} && checked(census.query(100, Scope::descendants), "mutated lineage") .resident[index(Kind::active_controlled)] .count == 1, "current-genome observation used birth genome or dropped a zero-carrier ancestor"); } void cohorts_and_death() { auto population = checked(CellularPopulation::create(cellular_descendants()), "census population"); const auto before = checked(CellularCensus::observe(*population), "individual wildlife census"); verify(before, population->state(), population.get()); const auto wildlife = std::ranges::find_if(population->state().lives(), [](const auto& life) { return life.account == LifeAccount{}; }); require(wildlife != population->state().lives().end(), "census needs real wildlife"); const auto genome = wildlife->identity.genome; const auto biology = population->state().snapshot(); const auto moved = checked(population->collapse(genome), "census collapse"); const auto cohort = checked(CellularCensus::observe(*population), "cohort census"); verify(cohort, population->state(), population.get()); const auto stock = checked(cohort.query(genome, Scope::exact_genome), "cohort stock"); require(moved > 0 && stock.resident[index(Kind::cohort_wildlife)].count == moved && stock.resident[index(Kind::individual_wildlife)].count == 0 && population->state().snapshot() == biology, "membership tags counted a second spendable population"); const auto& state = population->state(); const auto recovered = checked(CellularPopulation::restore(population->snapshot(), state.world(), state.region(), state.frame(), state.tick()), "cohort census restore"); require(std::ranges::equal( checked(CellularCensus::observe(*recovered), "restored cohort census").rows(), cohort.rows()), "restoring cohort tags changed census"); checked(population->materialize(genome), "census materialize"); require(std::ranges::equal( checked(CellularCensus::observe(*population), "materialized census").rows(), before.rows()), "conversion oscillation changed observed biology"); auto scarce = opening(7, false, 1); for (unsigned tick = 0; tick < 8192 && !scarce->lives().empty(); ++tick) step(*scarce); require(scarce->lives().empty() && scarce->archive().recorded() > 0, "death fixture never died"); const auto dead = checked(CellularCensus::observe(*scarce), "dead census"); verify(dead, *scarce); require(dead.rows().size() == 1 && dead.rows().front().totals == CellularCensusTotals{}, "corpse/detritus counted as a living or reproductive reserve"); const auto& dead_boundary = dead.boundary(); const auto dead_set = checked( CellularCensusSet::observe({dead_boundary.world, dead_boundary.frame, dead_boundary.tick, dead_boundary.owner_format, dead_boundary.rules, dead_boundary.quantum_microseconds}, std::array{dead_boundary.region}, std::array{dead}), "known absent scoped census"); require(checked(dead_set.query(dead.rows().front().genome, Scope::descendants), "known absent scoped lineage") == CellularCensusTotals{}, "retained zero-carrier lineage became unknown or living stock"); const auto empty = opening(1, true, 1, false); require(checked(CellularCensus::observe(*empty), "empty census").rows().empty(), "empty owner invented history"); auto large = opening(1, true, 256); verify(checked(CellularCensus::observe(*large), "capacity census"), *large); auto legacy_primitive = checked(PrimitiveLives::create({1, 1, 1, 1, {}, {{100, 100}}}), "legacy census opening"); auto legacy_habitat = checked(biology::CellularHabitat::create({1, 1, 1, 1000, 1000, {}}), "legacy census habitat"); const auto legacy = checked(CellularLives::create(*legacy_primitive, std::move(legacy_habitat), {{1000, 1000}, 0, 293150}), "legacy census owner"); const auto unsupported = CellularCensus::observe(*legacy); require(!unsupported && unsupported.error() == CellularCensusError::unsupported, "legacy rules silently reinterpreted by current census"); } Owner regional_founder(std::uint64_t region, std::uint64_t life, std::uint64_t genome, std::uint64_t event, std::uint64_t organism = 0) { auto owner = opening(1, true, 1, false, region); step(*owner); LifeRequest request{}; request.account.back() = 1; request.action = 1; return checked( owner->birth_candidate( request, {life, organism == 0 ? life : organism, genome, event, {owner->tick()}}), "regional census funded founder"); } CellularCensusSetBoundary set_boundary(const CellularCensus& census) { const auto& b = census.boundary(); return {b.world, b.frame, b.tick, b.owner_format, b.rules, b.quantum_microseconds}; } void regional_sets() { auto a = regional_founder(1, 100, 100, 100); auto b = regional_founder(2, 200, 200, 200); const auto ca = checked(CellularCensus::observe(*a), "set region A"); const auto cb = checked(CellularCensus::observe(*b), "set region B"); const std::array required{1, 2}; const auto boundary = set_boundary(ca); const auto observe = [&](const auto& observations) { return CellularCensusSet::observe(boundary, required, observations); }; const std::array reversed{cb, ca}; const auto set = checked(observe(reversed), "complete regional set"); require(set.regions()[0].boundary().region == 1 && set.regions()[1].boundary().region == 2 && set.boundary() == boundary, "regional set lost its boundary/canonical region order"); for (const auto scope : {Scope::exact_genome, Scope::descendants}) { require( checked(set.query(100, scope), "cross-region founder A") == direct(*a, 100, false) && checked(set.query(200, scope), "cross-region founder B") == direct(*b, 200, false), "missing local anchor hid a known lineage or manufactured stock"); const auto unknown = set.query(300, scope); require(!unknown && unknown.error() == CellularCensusError::unknown_genome, "unknown anchor became scoped absence"); } require(!set.query(100, Scope::invalid) && set.query(100, Scope::invalid).error() == CellularCensusError::invalid_scope, "set accepted invalid scope"); const auto refuse = [&](const auto& observations, CellularCensusSetError expected) { const auto result = observe(observations); require(!result && result.error() == expected, "set accepted incomplete/conflicting evidence"); }; refuse(std::array{ca}, CellularCensusSetError::coverage); refuse(std::array{ca, ca}, CellularCensusSetError::coverage); refuse(std::array{ca, cb, cb}, CellularCensusSetError::coverage); for (unsigned field = 0; field < 6; ++field) { auto wrong = boundary; switch (field) { case 0: ++wrong.world; break; case 1: ++wrong.frame; break; case 2: ++wrong.tick; break; case 3: ++wrong.owner_format; break; case 4: ++wrong.rules; break; case 5: ++wrong.quantum_microseconds; break; default: require(false, "invalid boundary-field test selector"); break; } const auto result = CellularCensusSet::observe(wrong, required, reversed); require(!result && result.error() == CellularCensusSetError::boundary, "regional set accepted foreign/stale/version-mismatched evidence"); } for (const auto bad : {std::array{0, 2}, {2, 1}, {1, 1}}) { const auto result = CellularCensusSet::observe(boundary, bad, reversed); require(!result && result.error() == CellularCensusSetError::invalid_request, "invalid required region set accepted"); } require(!CellularCensusSet::observe(boundary, {}, {}), "empty scope accepted"); auto duplicate = regional_founder(2, 100, 100, 100); refuse(std::array{ca, checked(CellularCensus::observe(*duplicate), "duplicate organism")}, CellularCensusSetError::resident_conflict); step(*b); refuse(std::array{ca, checked(CellularCensus::observe(*b), "stale boundary")}, CellularCensusSetError::boundary); require(checked(set.query(200, Scope::exact_genome), "detached region query") == direct(*regional_founder(2, 200, 200, 200), 200, false), "later biology mutated the detached set"); std::vector empty_regions; std::vector ids; for (std::uint64_t region = 1; region <= max_cellular_census_regions + 1; ++region) { auto empty = opening(1, true, 1, false, region); step(*empty); empty_regions.push_back(checked(CellularCensus::observe(*empty), "empty region")); ids.push_back(region); } require(CellularCensusSet::observe(boundary, std::span{ids}.first(64), std::span{empty_regions}.first(64)) .has_value() && !CellularCensusSet::observe(boundary, ids, empty_regions), "regional query work bound not enforced"); } void conflicting_regional_genomes() { auto a = regional_founder(1, 100, 100, 100); auto b = regional_founder(2, 200, 200, 200); // Independently valid owners can allocate the same successor ID. Shared // world identity must not merge their different immutable parent records. for (auto* owner : {a.get(), b.get()}) { bool applied = false; for (unsigned tick = 0; tick < 8192 && !applied; ++tick) { const auto& life = owner->lives().front(); std::vector choices; if (life.mutation.offer) { const auto& offer = *life.mutation.offer; for (const auto& candidate : offer.candidates) { const auto genome = checked(owner->archive().find(life.identity.genome), "set conflict mutation genome"); if (biology::fund_mutation(genome, offer.context, offer, candidate.operator_id, 900, 900, life.reserve)) { choices.push_back({life.identity.life, life.mutation.event_index, life.mutation.offer_tick, 900, candidate.operator_id}); break; } } } const std::array intent{{{life.identity.life, {}}}}; const auto result = checked(owner->advance_tick_with_mutations(intent, choices), "set conflict funded mutation"); applied = !result.mutations.empty() && result.mutations.front().result == CellularMutationResult::applied; } require(applied, "set conflict fixture did not fund a mutation"); } while (a->tick() < b->tick()) step(*a); while (b->tick() < a->tick()) step(*b); const auto ca = checked(CellularCensus::observe(*a), "conflict census A"); const auto cb = checked(CellularCensus::observe(*b), "conflict census B"); const auto result = CellularCensusSet::observe( set_boundary(ca), std::array{1, 2}, std::array{ca, cb}); require(!result && result.error() == CellularCensusSetError::genome_conflict, "different immutable records merged under one world genome ID"); } void maturing_owners() { auto source = cellular_descendants(); auto boundary = checked(source->migration_upgrade_candidate(), "maturing census boundary"); auto owner = checked(boundary->maturation_upgrade_candidate(), "maturing census owner"); const auto raw = checked(CellularCensus::observe(*owner), "maturing raw census"); verify(raw, *owner); auto population = checked(CellularPopulation::create(std::move(owner)), "maturing census population"); const auto wildlife = std::ranges::find_if(population->state().lives(), [](const auto& life) { return life.account == LifeAccount{}; }); require(wildlife != population->state().lives().end(), "maturing census lacks real juvenile"); const auto genome = wildlife->identity.genome; checked(population->collapse(genome), "maturing census collapse"); const auto biology = population->state().snapshot(); const auto detached = checked(CellularCensus::observe(*population), "detached maturing census"); verify(detached, population->state(), population.get()); const auto held_rows = std::vector{detached.rows().begin(), detached.rows().end()}; checked(population->materialize(genome), "maturing census materialize"); verify(checked(CellularCensus::observe(*population), "materialized maturing census"), population->state(), population.get()); checked(population->collapse(genome), "maturing census recollapse"); require(population->state().snapshot() == biology, "census conversion changed physical stock"); bool grew = false; for (unsigned tick = 0; tick < 256; ++tick) { std::vector controlled; for (const auto& life : population->state().lives()) if (life.account != LifeAccount{}) { CellularLifeIntent frozen{life.identity.life, {}}; frozen.intent.frozen = true; controlled.push_back(frozen); } const auto executed = checked(population->advance_tick(controlled), "census funded growth"); grew |= !executed.growth.empty(); const auto saved = population->snapshot(); const auto census = checked(CellularCensus::observe(*population), "growing cohort census"); verify(census, population->state(), population.get()); verify(checked(CellularCensus::observe(population->state()), "growing raw census"), population->state()); require(population->snapshot() == saved && std::ranges::equal(detached.rows(), held_rows), "census advanced biology or rewrote a detached growth observation"); if (tick == 127) { const auto& state = population->state(); population = checked(CellularPopulation::restore(saved, state.world(), state.region(), state.frame(), state.tick()), "growing census restore"); require( std::ranges::equal( checked(CellularCensus::observe(*population), "restored growing census").rows(), census.rows()), "restore changed growing census"); } } require(grew && detached.boundary().rules == cellular_lives_maturation_rules && detached.boundary().owner_format == 7 && detached.boundary().population_format == 2, "maturation census did not exercise real growth or retained wrong versions"); } void maturing_regional_sets() { auto a = checked( checked(regional_founder(1, 100, 100, 100)->migration_upgrade_candidate(), "set A boundary") ->maturation_upgrade_candidate(), "set A maturation"); auto old_b = checked(regional_founder(2, 200, 200, 200)->migration_upgrade_candidate(), "set B boundary"); auto b = checked(CellularPopulation::create( checked(old_b->maturation_upgrade_candidate(), "set B maturation")), "set B population"); const auto ca = checked(CellularCensus::observe(*a), "maturing set A"); const auto cb = checked(CellularCensus::observe(*b), "maturing set B"); const auto old = checked(CellularCensus::observe(*old_b), "retained rules-8 set B"); const std::array regions{1, 2}; const auto set = checked(CellularCensusSet::observe(set_boundary(ca), regions, std::array{cb, ca}), "complete maturing set"); require(checked(set.query(100, Scope::descendants), "maturing set founder A") == direct(*a, 100, true) && checked(set.query(200, Scope::exact_genome), "maturing set founder B") == direct(b->state(), 200, false, b.get()), "maturing scoped set invented or lost stock"); const auto mixed = CellularCensusSet::observe(set_boundary(ca), regions, std::array{ca, old}); require(!mixed && mixed.error() == CellularCensusSetError::boundary, "mixed rules-8/rules-9 owners became a compatible complete set"); const auto converse = CellularCensusSet::observe(set_boundary(old), regions, std::array{ca, old}); require(!converse && converse.error() == CellularCensusSetError::boundary, "rules-9 evidence was silently reinterpreted under a rules-8 boundary"); } } // namespace int main() try { presence_and_detachment(); mutation_and_retention(); cohorts_and_death(); regional_sets(); conflicting_regional_genomes(); maturing_owners(); maturing_regional_sets(); std::cout << "PASS local census presence, exact lineage queries, conversion and retention\n"; return 0; } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; }