M12.6: add bounded regional cellular census and lineage queries
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 2m33s
Foundation / checks (network) (push) Successful in 21m15s
Foundation / checks (client) (push) Successful in 22m8s
Foundation / checks (tsan) (push) Successful in 25m2s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (checks) (push) Canceled after 38m24s
Foundation / checks (postgres) (push) Canceled after 38m24s

This commit is contained in:
dingo
2026-10-18 13:57:52 -04:00
parent df680810af
commit da39fc8095
17 changed files with 757 additions and 4 deletions
+231
View File
@@ -0,0 +1,231 @@
#include "evo/server/lifecycle/cellular_census.hpp"
#include "fixtures/cellular_ecology.hpp"
#include <limits>
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<std::size_t>(kind);
}
Inventory sum(Inventory left, const Inventory& right) {
require(right.energy.value <= std::numeric_limits<std::uint64_t>::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<std::uint64_t>::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<std::size_t>(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<CellularLifeIntent> 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<CellularMutationSelection> 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<CellularLifeIntent, 1> 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 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");
}
} // namespace
int main() try {
presence_and_detachment();
mutation_and_retention();
cohorts_and_death();
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;
}