#include "evo/biology/budding.hpp" #include "evo/server/lifecycle/cellular_population_batch.hpp" #include "fixtures/cellular_ecology.hpp" #include #include namespace { using namespace evo; using namespace evo::server; using namespace evo::fixture; Inventory stocked(std::uint64_t amount = 1'000'000'000'000ULL) { Inventory result{{amount}, {}}; for (auto& channel : result.matter) channel.value = amount; return result; } void construction_and_saved_choices() { const auto genome = checked(biology::primitive_founder(1, 1, biology::primitive_founder_v3), "growth genome"); const biology::GrowthState start{400, 37}; const biology::EnvironmentState environment{293'150, stocked()}; const auto budget = checked(biology::metabolic_budget(genome, start, environment, 5), "growth cap"); require(budget.construction.after == biology::GrowthState{405, 37}, "growth cap/damage changed"); require(checked(biology::metabolic_budget(genome, start, environment, 0), "zero cap") .construction.after == start, "zero cap grew"); for (std::size_t channel = 0; channel < 5; ++channel) { auto available = environment; if (channel == 0) available.available.energy.value = 0; else available.available.matter[channel - 1].value = 0; require(checked(biology::metabolic_budget(genome, start, available, 5), "starved growth") .construction.after == start, "missing nutrient/energy still grew"); } const auto one = checked(biology::metabolic_budget(genome, start, environment, 1), "one step"); auto limited = environment; limited.available = one.construction.intake; require(checked(biology::metabolic_budget(genome, start, limited, 5), "partial funds") .construction.after == one.construction.after, "partial funding did not choose largest affordable step"); const auto grown = checked(biology::metabolic_budget(genome, budget.construction.after, environment), "derived grown body"); for (std::size_t channel = 0; channel < 4; ++channel) require(budget.body.matter[channel].value + budget.construction.intake.matter[channel].value == grown.body.matter[channel].value + budget.construction.release.matter[channel].value, "construction channel did not close"); const biology::MutationContext context{17, 2, 1, 8, start, environment}; const auto offer = checked(biology::mutation_candidates(genome, context), "saved offer"); const biology::GrowthState current{700, 37}; for (const auto& choice : offer.candidates) { const auto transition = checked(biology::fund_mutation_at_growth(genome, context, offer, choice.operator_id, 2, 2, stocked(), current), "current-size choice"); const auto expected = checked(biology::metabolic_budget(transition.successor, current, environment), "current-size body"); require(transition.body == expected.body, "saved choice restored obsolete body"); require(transition.successor == checked(biology::select_mutation(genome, context, offer, choice.operator_id, 2, 2), "original draw"), "growth rerolled choice"); const auto old = checked( biology::fund_mutation(genome, context, offer, choice.operator_id, 2, 2, stocked()), "legacy choice"); require(old.body != transition.body, "current-growth fixture is not discriminating"); auto changed = context; changed.growth = current; require(!biology::fund_mutation_at_growth(genome, changed, offer, choice.operator_id, 2, 2, stocked(), current), "changed offer context accepted"); } } std::vector idle(const CellularLives& state, bool freeze = false, std::uint64_t bud = 0) { std::vector result; for (const auto& life : state.lives()) { CellularLifeIntent intent{life.identity.life, {}}; intent.intent.frozen = freeze && life.account != LifeAccount{}; intent.intent.bud = life.identity.life == bud && !intent.intent.frozen; result.push_back(intent); } return result; } Owner upgraded(Owner state) { require(!state->maturation_upgrade_candidate(), "format 6 bypassed explicit boundary upgrade"); auto boundary = checked(state->migration_upgrade_candidate(), "boundary upgrade"); const auto before = boundary->snapshot(); auto result = checked(boundary->maturation_upgrade_candidate(), "growth upgrade"); require(boundary->snapshot() == before && result->tick() == boundary->tick() && result->current_total() == boundary->current_total() && std::ranges::equal(result->lives(), boundary->lives()), "upgrade changed biology"); require(!result->maturation_upgrade_candidate() && !result->migration_upgrade_candidate(), "unsupported upgrade admitted"); return result; } struct Family { Owner state; std::uint64_t child{}; }; Family first_bud(bool maturation) { auto state = opening(7, true, 1); if (maturation) state = upgraded(std::move(state)); for (unsigned tick = 0; tick < 12'000; ++tick) { const auto executed = checked( state->advance_tick_with_mutations(idle(*state, false, tick % 100 == 99 ? 100 : 0), {}), "first funded bud tick"); for (const auto& bud : executed.buds) { if (bud.result != CellularBudResult::budded) continue; const auto child = std::ranges::find( state->lives(), bud.bud_life, [](const auto& life) { return life.identity.life; }); require(child != state->lives().end() && child->growth.realized_per_thousand < 1000 && child->account == LifeAccount{}, "bud is not a genuine juvenile"); require(std::ranges::none_of( executed.growth, [&](const auto& growth) { return growth.life_id == bud.bud_life; }), "newborn grew in birth tick"); return {std::move(state), bud.bud_life}; } } throw std::runtime_error("finite feeding never funded the first bud"); } // Geometry-only codec fixture: retain real funded biology, identities and all // five stocks. This is a placement fault seam, never an adult/resource shortcut. Owner relocated(const CellularLives& state, std::uint64_t id, biology::CellularPosition position) { auto bytes = state.snapshot(); const auto index = static_cast( std::ranges::find(state.lives(), id, [](const auto& life) { return life.identity.life; }) - state.lives().begin()); require(index < state.lives().size() && state.format_version() == 7, "relocation fixture"); const auto put = [&](std::size_t at, std::uint64_t value) { for (unsigned shift = 0; shift < 8; ++shift) bytes[at + shift] = static_cast(value >> (shift * 8U)); }; std::uint32_t sites = 0; for (unsigned shift = 0; shift < 4; ++shift) sites |= std::uint32_t{bytes[60 + shift]} << (shift * 8U); const auto row = 308 + sites * 8 + index * 504; put(row + 72, position.x_um); put(row + 80, position.y_um); std::uint64_t hash = 14695981039346656037ULL; for (const auto byte : std::span{bytes}.first(bytes.size() - 8)) hash = (hash ^ byte) * 1099511628211ULL; put(bytes.size() - 8, hash); return checked( CellularLives::restore(bytes, state.world(), state.region(), state.frame(), state.tick()), "geometry fixture restore"); } void frozen_construction() { auto family = first_bud(false); auto state = upgraded(std::move(family.state)); const auto founder = state->lives().front(); const auto genome = checked(state->archive().find(founder.identity.genome), "frozen genome"); const biology::GrowthState juvenile{400, 0}; const auto body = checked(biology::metabolic_budget(genome, juvenile, {293150, {}}), "frozen body").body; auto bytes = state->snapshot(); const auto put = [&](std::size_t at, std::uint64_t value, unsigned width = 8) { for (unsigned shift = 0; shift < width; ++shift) bytes[at + shift] = static_cast(value >> (shift * 8U)); }; // Offline-guard fixture only: a controlled subadult is not produced by the // current adult-founder flow. Return already-owned body matter to its reserve; // grant no stock/energy and leave the paid construction heat in the world. constexpr std::size_t row = 308 + 8; // One site, first life, format 7. put(row + 64, juvenile.realized_per_thousand, 4); for (std::size_t channel = 0; channel < 4; ++channel) { put(row + 96 + channel * 8, body.matter[channel].value); put(row + 136 + channel * 8, founder.reserve.matter[channel].value + founder.body.matter[channel].value - body.matter[channel].value); } std::uint64_t hash = 14695981039346656037ULL; for (const auto byte : std::span{bytes}.first(bytes.size() - 8)) hash = (hash ^ byte) * 1099511628211ULL; put(bytes.size() - 8, hash); state = checked(CellularLives::restore(bytes, state->world(), state->region(), state->frame(), state->tick()), "controlled subadult guard fixture"); const auto held = state->lives().front(); auto active = restore(*state); const auto active_tick = checked(active->advance_tick_with_mutations(idle(*active), {}), "active subadult"); require(std::ranges::any_of(active_tick.growth, [](const auto& value) { return value.life_id == 100; }), "offline fixture could not otherwise grow"); for (unsigned tick = 0; tick <= cellular_linger_ticks; ++tick) { checked(state->advance_tick(idle(*state, true)), "frozen subadult"); const auto& frozen = state->lives().front(); require(frozen.growth == held.growth && frozen.body == held.body && frozen.reserve == held.reserve && frozen.mutation == held.mutation, "frozen subadult advanced biology"); } require(state->lives().front().presence == CellularPresence::suspended, "subadult did not suspend"); state = restore(*state); const auto resumed = checked(state->advance_tick_with_mutations(idle(*state), {}), "subadult resume"); require(state->lives().front().presence == CellularPresence::active && state->lives().front().growth == held.growth && std::ranges::none_of(resumed.growth, [](const auto& value) { return value.life_id == 100; }), "suspended resume constructed in its first tick"); const auto next = checked(state->advance_tick_with_mutations(idle(*state), {}), "resumed growth"); require( std::ranges::any_of(next.growth, [](const auto& value) { return value.life_id == 100; }), "resumed active life failed to grow"); } void obstructed_expansion() { auto family = first_bud(false); for (unsigned tick = 0; tick < 3000; ++tick) checked(family.state->advance_tick(idle(*family.state)), "finite juvenile feeding"); auto state = upgraded(std::move(family.state)); state = relocated(*state, family.child, {2000, 2000}); for (unsigned tick = 0; tick < 200; ++tick) { auto clear = restore(*state); const auto before = *std::ranges::find(state->lives(), family.child, [](const auto& life) { return life.identity.life; }); const auto genome = checked(state->archive().find(before.identity.genome), "geometry genome"); const auto old_extent = checked(biology::derive_phenotype(genome, before.growth, {293150, {}}), "old geometry") .extent; const auto trial = checked(clear->advance_tick_with_mutations(idle(*clear, true), {}), "clear growth"); const auto grown = std::ranges::find(trial.growth, family.child, &CellularGrowthExecution::life_id); if (grown != trial.growth.end() && checked(biology::derive_phenotype(genome, grown->after, {293150, {}}), "expanded geometry") .extent > old_extent) { for (const auto position : {biology::CellularPosition{48000 - old_extent, 2000}, biology::CellularPosition{1000 - old_extent, 1000}}) { auto blocked = relocated(*state, family.child, position); const auto executed = checked(blocked->advance_tick_with_mutations(idle(*blocked, true), {}), "obstructed growth"); const auto after = std::ranges::find(blocked->lives(), family.child, [](const auto& life) { return life.identity.life; }); const auto step = std::ranges::find(executed.step.lives, family.child, &biology::CellularLifeStepResult::life_id); require(after != blocked->lives().end() && after->growth == before.growth && after->body == before.body && after->position == position && step != executed.step.lives.end() && after->reserve == step->candidate.reserve_after && std::ranges::none_of( executed.growth, [&](const auto& value) { return value.life_id == family.child; }), "blocked expansion spent, moved or grew"); require(restore(*blocked)->snapshot() == blocked->snapshot(), "blocked restore differs"); } return; } state = std::move(clear); } throw std::runtime_error("geometry fixture never exercised a funded extent increase"); } void funded_generations() { auto family = first_bud(true); auto& state = family.state; const auto opening_total = state->opening_total(); bool adult = false; std::size_t growth_count = 0; for (unsigned tick = 0; tick < 24'000; ++tick) { auto intentions = idle(*state, true, tick % 100 == 99 ? family.child : 0); // Separate the real bud from a possible twin using paid movement. if (tick < 20) for (auto& intent : intentions) if (intent.life_id != family.child && intent.life_id != 100) intent.intent.movement = {0, 1000}; const auto executed = checked(state->advance_tick_with_mutations(intentions, {}), "funded family tick"); growth_count += executed.growth.size(); require(state->current_total() == opening_total, "family stock did not close"); for (const auto& growth : executed.growth) require(growth.after.realized_per_thousand > growth.before.realized_per_thousand && growth.after.realized_per_thousand - growth.before.realized_per_thousand <= 5 && growth.after.damage_per_thousand == growth.before.damage_per_thousand, "growth rate or damage changed"); for (const auto& growth : executed.growth) { const auto step = std::ranges::find(executed.step.lives, growth.life_id, &biology::CellularLifeStepResult::life_id); const auto life = std::ranges::find(state->lives(), growth.life_id, [](const auto& value) { return value.identity.life; }); require(step != executed.step.lives.end() && life != state->lives().end(), "growth report identity"); require(life->reserve.energy.value + growth.intake.energy.value == step->candidate.reserve_after.energy.value, "growth energy did not spend reserve"); for (std::size_t channel = 0; channel < 4; ++channel) require(life->reserve.matter[channel].value + growth.intake.matter[channel].value == step->candidate.reserve_after.matter[channel].value + growth.release.matter[channel].value, "growth reserve channel did not close"); } for (const auto& life : state->lives()) { if (life.identity.life == family.child) adult |= life.growth.realized_per_thousand == 1000; if (life.identity.life == 100) require(life.growth == biology::GrowthState{1000, 0}, "offline founder changed"); } if (tick % 317 == 0) state = restore(*state); for (const auto& bud : executed.buds) { if (bud.parent_life != family.child || bud.result != CellularBudResult::budded) continue; require(adult && growth_count > 0, "offspring reproduced without funded adulthood"); std::cout << "funded juvenile reached adulthood and budded at tick " << state->tick() << ", growth steps " << growth_count << '\n'; return; } } throw std::runtime_error("funded juvenile never reproduced"); } void legacy_and_population_replay() { auto family = first_bud(false); auto legacy = restore(*family.state); const auto initial = std::ranges::find(legacy->lives(), family.child, [](const auto& life) { return life.identity.life; })->growth; for (unsigned tick = 0; tick < 400; ++tick) { const auto executed = checked(legacy->advance_tick_with_mutations(idle(*legacy, true), {}), "legacy no growth"); require(executed.growth.empty(), "rules 8 gained growth"); } require(std::ranges::find( legacy->lives(), family.child, [](const auto& life) { return life.identity.life; })->growth == initial, "rules 8 juvenile changed growth"); auto boundary = checked(family.state->migration_upgrade_candidate(), "population boundary"); auto population = checked(CellularPopulation::create(std::move(boundary)), "population create"); const auto genome = population->state().lives().front().identity.genome; checked(population->collapse(genome), "juvenile collapse"); const auto tags = std::vector{population->members().begin(), population->members().end()}; const auto sequence = population->conversion_sequence(); population = checked(population->maturation_upgrade_candidate(), "population growth upgrade"); require(std::ranges::equal(population->members(), tags) && population->conversion_sequence() == sequence, "upgrade lost cohort tags"); auto raw = restore(population->state()); bool grew = false; for (unsigned tick = 0; tick < 1024; ++tick) { auto all = checked(wildlife_intents(*raw, raw->tick() + 1), "raw wildlife intentions"); std::vector controlled; for (const auto& life : raw->lives()) if (life.account != LifeAccount{}) { CellularLifeIntent frozen{life.identity.life, {}}; frozen.intent.frozen = true; controlled.push_back(frozen); all.push_back(frozen); } std::ranges::sort(all, {}, &CellularLifeIntent::life_id); auto before = restore(*raw); const auto executed = checked(raw->advance_tick_with_mutations(all, {}), "raw maturation"); grew |= !executed.growth.empty(); checked(population->advance_tick(controlled), "cohort maturation"); require(raw->snapshot() == population->state().snapshot(), "cohort changed growth biology"); CellularBatch batch; batch.header = {raw->world(), raw->region(), raw->frame(), {}, {}, before->tick(), raw->tick()}; batch.header.predecessor[0] = 1; batch.header.predecessor_digest[0] = 2; batch.ticks.push_back({all, {}, {}}); const auto bytes = checked(encode_cellular_batch(batch), "growth trace encode"); const auto decoded = checked(decode_cellular_batch(bytes), "growth trace decode"); require( checked(replay_cellular_batch(*before, decoded), "growth trace replay")->snapshot() == raw->snapshot(), "recorded growth replay differs"); if (tick % 127 == 0) { population = checked(CellularPopulation::restore(population->snapshot(), raw->world(), raw->region(), raw->frame(), raw->tick()), "growing cohort restore"); raw = restore(*raw); } if (tick == 256) { const auto biology = population->state().snapshot(); checked(population->materialize(genome), "growing materialization"); checked(population->collapse(genome), "growing recollapse"); require(population->state().snapshot() == biology, "conversion reset growth"); } } require(grew, "raw/cohort comparison never exercised growth"); require(population_interval_safe(*raw), "suspended controlled owner refused rules 9 interval"); CellularBatchHeader header{raw->world(), raw->region(), raw->frame(), {}, {}, raw->tick(), raw->tick() + 64}; header.predecessor[0] = 1; header.predecessor_digest[0] = 2; auto inputs = checked(unattended_population_inputs(*raw, header), "growth interval inputs"); auto execution = checked(prepare_population_batch(*population, std::move(inputs)), "growth interval"); auto batch = checked(decode_population_batch(checked(encode_population_batch(execution.batch), "population trace encode")), "population trace decode"); require(checked(replay_population_batch(*population, batch), "population interval replay") ->snapshot() == execution.state->snapshot(), "population interval growth replay differs"); } void save(const char* path, std::span bytes) { std::ofstream file(path, std::ios::binary | std::ios::trunc); file.write(reinterpret_cast(bytes.data()), static_cast(bytes.size())); require(file.good(), "maturation checkpoint write"); } void continue_population(CellularPopulation& population) { bool grew = false; for (unsigned tick = 0; tick < 64; ++tick) { 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); } const auto executed = checked(population.advance_tick(controlled), "restart continuation"); grew |= !executed.growth.empty(); } require(grew, "restart continuation never grew"); } void checkpoint(const char* path, const char* expected) { auto family = first_bud(false); for (unsigned tick = 0; tick < 400; ++tick) checked(family.state->advance_tick(idle(*family.state, true)), "restart suspension"); auto population = checked(CellularPopulation::create(upgraded(std::move(family.state))), "restart population"); checked(population->collapse(population->state().lives().front().identity.genome), "restart cohort"); save(path, population->snapshot()); continue_population(*population); save(expected, population->snapshot()); } void resume(const char* path, const char* output) { std::ifstream file(path, std::ios::binary); require(file.good(), "restart checkpoint read"); const std::vector bytes{std::istreambuf_iterator{file}, {}}; require(bytes.size() >= cellular_population_header_bytes + 8, "restart checkpoint length"); // Test-only trusted fixture context; production recovery binds a manifest. std::uint64_t tick = 0; for (unsigned shift = 0; shift < 8; ++shift) tick |= std::uint64_t{bytes[56 + shift]} << (shift * 8U); auto population = checked(CellularPopulation::restore(bytes, 207, 1, 1, tick), "fresh-process restore"); continue_population(*population); save(output, population->snapshot()); } } // 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; } require(argc == 1, "unknown maturation test arguments"); construction_and_saved_choices(); frozen_construction(); obstructed_expansion(); funded_generations(); legacy_and_population_replay(); return 0; } catch (const std::exception& error) { std::fprintf(stderr, "%s\n", error.what()); return 1; }