Foundation / checks (ecs) (push) Successful in 2m26s
Foundation / checks (deploy) (push) Successful in 4m57s
Foundation / checks (network) (push) Successful in 20m53s
Foundation / checks (client) (push) Successful in 22m12s
Foundation / checks (tsan) (push) Successful in 39m45s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 45m41s
Foundation / checks (checks) (push) Canceled after 45m43s
309 lines
16 KiB
C++
309 lines
16 KiB
C++
#include "evo/server/lifecycle/cellular_census_set.hpp"
|
|
#include "evo/server/lifecycle/cellular_population_batch.hpp"
|
|
#include "fixtures/cellular_ecology.hpp"
|
|
|
|
#include <fstream>
|
|
#include <iterator>
|
|
|
|
namespace {
|
|
using namespace evo;
|
|
using namespace evo::server;
|
|
using namespace evo::fixture;
|
|
using Population = std::unique_ptr<CellularPopulation>;
|
|
std::vector<CellularLifeIntent> controlled(const CellularLives& state, bool frozen = false,
|
|
bool bud = false) {
|
|
std::vector<CellularLifeIntent> result;
|
|
for (const auto& life : state.lives())
|
|
if (life.account != LifeAccount{}) {
|
|
CellularLifeIntent input{life.identity.life, {}};
|
|
input.intent.frozen = frozen;
|
|
input.intent.bud = bud && !frozen;
|
|
result.push_back(input);
|
|
}
|
|
return result;
|
|
}
|
|
void paired(CellularPopulation& population, CellularLives& raw, bool frozen = false,
|
|
bool bud = false, std::span<const CellularMutationSelection> selections = {}) {
|
|
auto inputs = controlled(raw, frozen, bud);
|
|
auto all = checked(wildlife_intents(raw, raw.tick() + 1), "paired wildlife");
|
|
all.insert(all.end(), inputs.begin(), inputs.end());
|
|
std::ranges::sort(all, {}, &CellularLifeIntent::life_id);
|
|
const auto expected =
|
|
checked(raw.advance_tick_with_mutations(all, selections), "raw plan tick");
|
|
const auto actual = checked(population.advance_tick(inputs, selections), "cohort plan tick");
|
|
require(actual.mutations == expected.mutations && actual.buds == expected.buds &&
|
|
actual.growth == expected.growth &&
|
|
population.state().snapshot() == raw.snapshot() &&
|
|
raw.current_total() == raw.opening_total(),
|
|
"cohort changed inherited biology or conserved inventory");
|
|
}
|
|
Population legacy() {
|
|
auto state = opening(7, true, 1, false);
|
|
checked(state->advance_tick({}), "population source clock");
|
|
LifeAccount account{};
|
|
account.back() = 1;
|
|
state = checked(state->birth_candidate({account, 1}, {100, 100, 100, 100, {state->tick()}}),
|
|
"population source founder");
|
|
state = checked(state->migration_upgrade_candidate(), "source boundary upgrade");
|
|
state = checked(state->maturation_upgrade_candidate(), "source maturation upgrade");
|
|
for (unsigned tick = 0; tick < 30'000 && state->lives().size() == 1; ++tick)
|
|
checked(state->advance_tick(controlled(*state, false, true)), "fund legacy bud");
|
|
require(state->lives().size() > 1, "no real legacy wildlife for retained tags");
|
|
auto result = checked(CellularPopulation::create(std::move(state), {false, 900, 1600}),
|
|
"legacy exact population");
|
|
checked(result->collapse(100), "legacy whole-group collapse");
|
|
return result;
|
|
}
|
|
void choose(CellularPopulation& population, CellularLives& raw, std::uint64_t successor) {
|
|
for (unsigned tick = 0; tick < 60'000; ++tick) {
|
|
const auto life = raw.lives().front();
|
|
require(life.identity.life == 100, "plan parent died before selection");
|
|
if (!life.mutation.offer) {
|
|
paired(population, raw);
|
|
continue;
|
|
}
|
|
const auto& offer = *life.mutation.offer;
|
|
const auto found = std::ranges::find_if(offer.candidates, [](const auto& candidate) {
|
|
return candidate.target == biology::Capability::membrane ||
|
|
candidate.target == biology::Capability::target_size;
|
|
});
|
|
const bool structural = found != offer.candidates.end();
|
|
const CellularMutationSelection selection{
|
|
100, life.mutation.event_index, life.mutation.offer_tick, successor,
|
|
structural ? found->operator_id : std::uint16_t{0}};
|
|
paired(population, raw, false, false, std::span{&selection, 1});
|
|
if (structural) {
|
|
require(raw.lives().front().identity.genome == 100 &&
|
|
raw.lives().front().offspring_genome == successor,
|
|
"cohort selection rewrote the expressed parent");
|
|
return;
|
|
}
|
|
}
|
|
throw std::runtime_error("finite food did not earn structural cohort plan");
|
|
}
|
|
void census(const CellularPopulation& population, const CellularLives& raw) {
|
|
const auto individual = checked(CellularCensus::observe(raw), "raw plan census");
|
|
const auto cohort = checked(CellularCensus::observe(population), "cohort plan census");
|
|
require(cohort.boundary().projection_version == 2 && cohort.boundary().population_format == 3 &&
|
|
cohort.rows().size() == individual.rows().size(),
|
|
"cohort lost its inherited census boundary");
|
|
for (std::size_t row = 0; row < cohort.rows().size(); ++row) {
|
|
auto expected = individual.rows()[row];
|
|
auto actual = cohort.rows()[row];
|
|
auto& stock = actual.totals.resident[0];
|
|
const auto& tagged = actual.totals.resident[1];
|
|
stock.count += tagged.count;
|
|
stock.body.energy.value += tagged.body.energy.value;
|
|
stock.reserve.energy.value += tagged.reserve.energy.value;
|
|
for (std::size_t channel = 0; channel < stock.body.matter.size(); ++channel) {
|
|
stock.body.matter[channel].value += tagged.body.matter[channel].value;
|
|
stock.reserve.matter[channel].value += tagged.reserve.matter[channel].value;
|
|
}
|
|
actual.totals.resident[1] = {};
|
|
actual.totals.living_plan_holders[0] += actual.totals.living_plan_holders[1];
|
|
actual.totals.living_plan_holders[1] = 0;
|
|
require(actual == expected, "cohort census changed stock or inherited-plan references");
|
|
}
|
|
}
|
|
Population family() {
|
|
auto source = legacy();
|
|
const auto bytes = source->snapshot();
|
|
const auto tags =
|
|
std::vector<CellularCohortMember>{source->members().begin(), source->members().end()};
|
|
const auto sequence = source->conversion_sequence();
|
|
auto population =
|
|
checked(source->inheritance_upgrade_candidate(), "tagged inheritance upgrade");
|
|
require(source->snapshot() == bytes && population->format_version() == 3 &&
|
|
population->policy() == source->policy() &&
|
|
population->conversion_sequence() == sequence &&
|
|
std::ranges::equal(population->members(), tags) &&
|
|
!population->inheritance_upgrade_candidate() &&
|
|
population->state().current_total() == source->state().current_total(),
|
|
"population upgrade reset representation or biology");
|
|
auto raw = checked(source->state().inheritance_upgrade_candidate(), "independent raw upgrade");
|
|
require(raw->snapshot() == population->state().snapshot(),
|
|
"nested upgrade changed raw biology");
|
|
choose(*population, *raw, 1'000'000);
|
|
census(*population, *raw);
|
|
require(!population_interval_safe(*raw),
|
|
"active controlled parent entered unattended interval");
|
|
bool born = false;
|
|
for (unsigned tick = 0; tick < 30'000 && !born; ++tick) {
|
|
paired(*population, *raw, false, true);
|
|
born = std::ranges::any_of(raw->lives(), [](const auto& life) {
|
|
return life.parent_life == 100 && life.identity.genome == 1'000'000;
|
|
});
|
|
}
|
|
require(born, "cohort parent did not fund inherited child");
|
|
const auto biology = population->state().snapshot();
|
|
auto tagged =
|
|
checked(population->conversion_candidate(1'000'000, true), "tag inherited children");
|
|
require(population->state().snapshot() == biology && tagged->state().snapshot() == biology &&
|
|
tagged->conversion_sequence() == population->conversion_sequence() + 1,
|
|
"whole-group conversion changed inherited biology");
|
|
population = std::move(tagged);
|
|
census(*population, *raw);
|
|
const auto conversion_base = population->snapshot();
|
|
CellularBatchHeader cut{raw->world(), raw->region(), raw->frame(), {},
|
|
{}, raw->tick(), raw->tick()};
|
|
cut.predecessor[0] = 1;
|
|
cut.predecessor_digest[0] = 2;
|
|
auto conversion = checked(prepare_population_conversion(*population, cut, {1'000'000, false}),
|
|
"recorded inherited conversion");
|
|
auto recorded = checked(decode_population_batch(checked(
|
|
encode_population_batch(conversion.batch), "conversion encode")),
|
|
"conversion decode");
|
|
require(recorded.format_version == 3 && population->snapshot() == conversion_base &&
|
|
checked(replay_population_batch(*population, recorded), "conversion replay")
|
|
->snapshot() == conversion.state->snapshot() &&
|
|
conversion.state->state().snapshot() == raw->snapshot(),
|
|
"recorded inherited conversion changed biology or representation result");
|
|
recorded.format_version = 2;
|
|
require(!replay_population_batch(*population, recorded),
|
|
"legacy conversion trace admitted envelope 3");
|
|
population = std::move(conversion.state);
|
|
checked(population->collapse(1'000'000), "retag inherited children after conversion");
|
|
choose(*population, *raw, 500'000);
|
|
census(*population, *raw);
|
|
for (unsigned tick = 0; tick <= cellular_linger_ticks; ++tick)
|
|
paired(*population, *raw, true);
|
|
require(population_interval_safe(*raw), "suspended inherited owner refused interval");
|
|
census(*population, *raw);
|
|
const auto observed = checked(CellularCensus::observe(*population), "suspended cohort census");
|
|
const auto plan =
|
|
checked(observed.query(500'000, CellularCensusScope::exact_genome), "unborn plan query");
|
|
require(plan.resident == std::array<CellularCensusStock, 5>{} &&
|
|
plan.living_plan_holders[static_cast<std::size_t>(
|
|
CellularCensusKind::suspended_controlled)] == 1,
|
|
"suspension erased inherited plan or invented expressed stock");
|
|
return population;
|
|
}
|
|
CellularBatchHeader header(const CellularLives& state, unsigned ticks) {
|
|
CellularBatchHeader result{state.world(), state.region(), state.frame(), {}, {},
|
|
state.tick(), state.tick() + ticks};
|
|
result.predecessor[0] = 1;
|
|
result.predecessor_digest[0] = 2;
|
|
return result;
|
|
}
|
|
void intervals(CellularPopulation& population) {
|
|
const auto before = population.snapshot();
|
|
const auto h = header(population.state(), 64);
|
|
const auto inputs =
|
|
checked(unattended_population_inputs(population.state(), h), "plan interval inputs");
|
|
require(inputs.format_version == cellular_batch_inheritance_format,
|
|
"wrong inherited raw trace");
|
|
auto executed = checked(prepare_population_batch(population, inputs), "inherited interval");
|
|
auto trace = checked(decode_population_batch(
|
|
checked(encode_population_batch(executed.batch), "plan trace encode")),
|
|
"plan trace decode");
|
|
require(trace.format_version == 3 && population.snapshot() == before &&
|
|
checked(replay_population_batch(population, trace), "plan interval replay")
|
|
->snapshot() == executed.state->snapshot(),
|
|
"inherited interval replay or source mutation");
|
|
auto raw = restore(population.state());
|
|
for (unsigned tick = 0; tick < 64; ++tick)
|
|
paired(population, *raw, true);
|
|
require(population.snapshot() == executed.state->snapshot(),
|
|
"recorded inherited interval differs from raw/control execution");
|
|
census(population, *raw);
|
|
auto automatic =
|
|
checked(CellularPopulation::create(restore(population.state()), {true, 900, 1600}),
|
|
"automatic inherited population");
|
|
const auto auto_inputs =
|
|
checked(unattended_population_inputs(automatic->state(), header(automatic->state(), 64)),
|
|
"automatic inherited inputs");
|
|
const auto auto_run =
|
|
checked(prepare_population_batch(*automatic, auto_inputs), "automatic inherited interval");
|
|
require(
|
|
checked(replay_population_batch(*automatic, auto_run.batch), "automatic inherited replay")
|
|
->snapshot() == auto_run.state->snapshot(),
|
|
"automatic inherited membership replay differs");
|
|
auto auto_raw = restore(automatic->state());
|
|
for (unsigned tick = 0; tick < 64; ++tick)
|
|
paired(*automatic, *auto_raw, true);
|
|
const auto wildlife = std::ranges::count_if(
|
|
auto_raw->lives(), [](const auto& life) { return life.account == LifeAccount{}; });
|
|
require(wildlife > 0 && automatic->members().size() == static_cast<std::size_t>(wildlife) &&
|
|
automatic->snapshot() == auto_run.state->snapshot(),
|
|
"automatic policy did not retain all real inherited wildlife");
|
|
const auto untouched = population.snapshot();
|
|
trace.biology.format_version = cellular_batch_format;
|
|
require(!encode_population_batch(trace) && !replay_population_batch(population, trace) &&
|
|
population.snapshot() == untouched,
|
|
"old trace admitted inherited biology");
|
|
trace = executed.batch;
|
|
++trace.boundaries.front().sequence;
|
|
require(!replay_population_batch(*checked(CellularPopulation::restore(before, h.world, h.region,
|
|
h.frame, h.begin_tick),
|
|
"unaltered interval base"),
|
|
trace),
|
|
"altered inherited membership boundary accepted");
|
|
const auto restored =
|
|
checked(CellularPopulation::restore(population.snapshot(), h.world, h.region, h.frame,
|
|
population.state().tick()),
|
|
"inherited population restore");
|
|
require(restored->snapshot() == population.snapshot(),
|
|
"retained envelope did not re-encode exactly");
|
|
}
|
|
void codec_boundaries(const CellularPopulation& population) {
|
|
const auto put = [](std::vector<std::uint8_t>& bytes, std::size_t offset, std::uint64_t value,
|
|
unsigned width) {
|
|
for (unsigned i = 0; i < width; ++i)
|
|
bytes.at(offset + i) = static_cast<std::uint8_t>(value >> (8U * i));
|
|
};
|
|
auto bytes = population.snapshot();
|
|
const auto& state = population.state();
|
|
for (const auto version : {1U, 2U, 4U}) {
|
|
auto false_version = bytes;
|
|
false_version.at(7) = static_cast<std::uint8_t>('0' + version);
|
|
put(false_version, 8, version, 4);
|
|
std::uint64_t hash = 14695981039346656037ULL;
|
|
for (const auto byte : std::span{false_version}.first(false_version.size() - 8))
|
|
hash = (hash ^ byte) * 1099511628211ULL;
|
|
put(false_version, false_version.size() - 8, hash, 8);
|
|
require(!CellularPopulation::restore(false_version, state.world(), state.region(),
|
|
state.frame(), state.tick()),
|
|
"inherited owner relabeled with old/unknown envelope");
|
|
}
|
|
bytes.pop_back();
|
|
require(!CellularPopulation::restore(bytes, state.world(), state.region(), state.frame(),
|
|
state.tick()),
|
|
"truncated retained envelope accepted");
|
|
}
|
|
void save(const char* path, std::span<const std::uint8_t> bytes) {
|
|
std::ofstream output(path, std::ios::binary | std::ios::trunc);
|
|
output.write(reinterpret_cast<const char*>(bytes.data()),
|
|
static_cast<std::streamsize>(bytes.size()));
|
|
require(output.good(), "retained population write");
|
|
}
|
|
} // namespace
|
|
int main(int argc, char** argv) try {
|
|
if (argc == 4 && std::string_view{argv[1]} == "--resume") {
|
|
std::ifstream input(argv[2], std::ios::binary);
|
|
require(input.good(), "retained population read");
|
|
const std::vector<std::uint8_t> bytes{std::istreambuf_iterator<char>{input}, {}};
|
|
require(bytes.size() >= cellular_population_header_bytes + 8, "retained restart header");
|
|
std::uint64_t tick{};
|
|
for (unsigned i = 0; i < 8; ++i)
|
|
tick |= std::uint64_t{bytes[56 + i]} << (8U * i);
|
|
auto population = checked(CellularPopulation::restore(bytes, 207, 1, 1, tick),
|
|
"fresh inherited population");
|
|
intervals(*population);
|
|
save(argv[3], population->snapshot());
|
|
return 0;
|
|
}
|
|
require(argc == 1 || (argc == 4 && std::string_view{argv[1]} == "--checkpoint"),
|
|
"unknown population plan arguments");
|
|
auto population = family();
|
|
codec_boundaries(*population);
|
|
if (argc == 4)
|
|
save(argv[2], population->snapshot());
|
|
intervals(*population);
|
|
if (argc == 4)
|
|
save(argv[3], population->snapshot());
|
|
return 0;
|
|
} catch (const std::exception& error) {
|
|
std::cerr << error.what() << '\n';
|
|
return 1;
|
|
}
|