#include "evo/server/lifecycle/cellular_census.hpp" #include "evo/server/lifecycle/cellular_migration.hpp" #include "fixtures/cellular_ecology.hpp" #include #include namespace { using namespace evo; using namespace evo::server; using namespace evo::fixture; std::vector idle(const CellularLives& state, bool frozen = false, bool bud = false, bool moving = false) { std::vector result; for (const auto& life : state.lives()) { CellularLifeIntent input{life.identity.life, {}}; if (life.account != LifeAccount{}) { input.intent.frozen = frozen; input.intent.bud = bud && !frozen; if (moving && !frozen) input.intent.movement = {1000, 0}; } result.push_back(input); } return result; } const CellularOwnedLife& parent(const CellularLives& state) { const auto life = std::ranges::find(state.lives(), 100, [](const auto& cell) { return cell.identity.life; }); require(life != state.lives().end(), "inheritance parent missing"); return *life; } void closed(const CellularLives& state) { require(state.current_total() == state.opening_total(), "inheritance stock did not close"); } Owner initial() { auto old = opening(7, true, 1); require(!old->inheritance_upgrade_candidate(), "inheritance skipped prior versions"); old = checked(old->migration_upgrade_candidate(), "boundary upgrade"); require(!old->inheritance_upgrade_candidate(), "inheritance skipped maturation"); old = checked(old->maturation_upgrade_candidate(), "maturation upgrade"); const auto original = old->snapshot(); auto state = checked(old->inheritance_upgrade_candidate(), "inheritance upgrade"); require(old->snapshot() == original && state->tick() == old->tick() && state->archive().encode() == old->archive().encode() && state->current_total() == old->current_total() && parent(*state).identity == parent(*old).identity && parent(*state).body == parent(*old).body && parent(*state).reserve == parent(*old).reserve && parent(*state).offspring_genome == parent(*old).identity.genome && state->offspring_revisions().empty(), "upgrade changed biology"); require(state->format_version() == 8 && state->rules_version() == 10 && !state->inheritance_upgrade_candidate(), "inheritance version boundary"); require(restore(*state)->snapshot() == state->snapshot(), "initial inheritance restore"); return state; } void choose_structure(Owner& state, std::uint64_t successor) { for (unsigned tick = 0; tick < 60'000; ++tick) { const auto before = parent(*state); if (before.mutation.offer) { const auto offer = *before.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 selected = found != offer.candidates.end(); const CellularMutationSelection selection{ 100, before.mutation.event_index, before.mutation.offer_tick, successor, selected ? found->operator_id : std::uint16_t{0}}; // Stale and reused identities cannot spend a tick or consume the offer. const auto bytes = state->snapshot(); auto stale = selection; ++stale.event_index; require(!state->advance_tick_with_mutations(idle(*state), std::span{&stale, 1}) && state->snapshot() == bytes, "stale plan selection changed state"); stale = selection; stale.successor_id = before.identity.genome; require(!state->advance_tick_with_mutations(idle(*state), std::span{&stale, 1}) && state->snapshot() == bytes, "reused plan identity changed state"); auto control = restore(*state); checked(control->advance_tick_with_mutations(idle(*control), {}), "unselected control"); const auto result = checked(state->advance_tick_with_mutations(idle(*state), std::span{&selection, 1}), "plan selection tick"); require(result.mutations.size() == 1 && result.mutations.front().result == (selected ? CellularMutationResult::offspring_plan_applied : CellularMutationResult::declined), "plan selection outcome"); const auto& cell = parent(*state); const auto& reference = parent(*control); require(cell.identity == reference.identity && cell.body == reference.body && cell.reserve == reference.reserve && cell.growth == reference.growth && cell.position == reference.position && state->heat() == control->heat() && state->nursery() == control->nursery() && state->habitat().snapshot() == control->habitat().snapshot(), "offspring selection rebuilt or charged the parent's body"); if (selected) { const auto& revision = state->offspring_revisions().back(); require(cell.offspring_genome == successor && revision.life == 100 && revision.previous == before.offspring_genome && revision.successor == successor && revision.tick == state->tick() && checked(state->archive().current_genome(100), "expressed archive") == before.identity.genome && checked(state->archive().living_carriers(successor), "unborn carriers") == 0, "offspring plan mistaken for an expressed organism"); require(restore(*state)->snapshot() == state->snapshot(), "selected plan restore"); closed(*state); return; } } else checked(state->advance_tick_with_mutations(idle(*state), {}), "earned offer tick"); closed(*state); } throw std::runtime_error("finite feeding never earned a structural offspring choice"); } std::uint64_t bud(Owner& state) { for (unsigned tick = 0; tick < 30'000; ++tick) { const auto plan = parent(*state).offspring_genome; const auto result = checked(state->advance_tick_with_mutations(idle(*state, false, true), {}), "inherited bud tick"); closed(*state); for (const auto& outcome : result.buds) { if (outcome.parent_life != 100 || outcome.result != CellularBudResult::budded) continue; const auto child = std::ranges::find(state->lives(), outcome.bud_life, [](const auto& life) { return life.identity.life; }); require(child != state->lives().end() && child->identity.genome == plan && child->offspring_genome == plan && child->parent_life == 100 && child->account == LifeAccount{} && std::ranges::none_of(result.growth, [&](const auto& growth) { return growth.life_id == child->identity.life; }), "newborn plan/body identity or phase"); const auto genome = checked(state->archive().find(plan), "inherited genome"); require(checked(biology::metabolic_budget(genome, child->growth, {293150, {}}), "inherited body") .body == child->body, "child body uses parent genome"); return child->identity.life; } } throw std::runtime_error("finite parent never funded inherited offspring"); } Owner family() { auto state = initial(); choose_structure(state, 1'000'000); bud(state); return state; } void continuation(CellularLives& state) { bool maintained = false; const auto plan = parent(state).offspring_genome; const auto history = state.offspring_revisions().size(); for (unsigned tick = 0; tick < 64; ++tick) { checked(state.advance_tick_with_mutations(idle(state, true), {}), "inherited continuation"); for (const auto& feedback : state.last_tick_feedback()) if (feedback.life_id != 100) maintained |= feedback.maintenance_cost.value > 0; require(parent(state).offspring_genome == plan && state.offspring_revisions().size() == history, "continuation lost retained plan"); closed(state); } // Maximal funded birth can already be fully grown; maintenance must still run. require(maintained, "inherited continuation skipped offspring metabolism"); } void put(std::vector& bytes, std::size_t at, std::uint64_t value, unsigned width = 8) { for (unsigned offset = 0; offset < width; ++offset) bytes.at(at + offset) = static_cast(value >> (offset * 8U)); } void reseal(std::vector& bytes) { std::uint64_t value = 14695981039346656037ULL; for (const auto byte : std::span{bytes}.first(bytes.size() - 8)) value = (value ^ byte) * 1099511628211ULL; put(bytes, bytes.size() - 8, value); } void codecs_and_siblings() { auto state = family(); const auto first_child = std::ranges::find_if( state->lives(), [](const auto& life) { return life.parent_life == 100; }); require(first_child != state->lives().end(), "missing first child"); const auto old_child = first_child->identity; const auto bytes = state->snapshot(); const auto history = bytes.size() - 8 - state->offspring_revisions().size() * 32; // One founding site, owner-8 header, then life rows. All corruptions carry a // valid outer checksum; restore must establish biological/history semantics. const auto corrupt = [&](std::size_t at, std::uint64_t value, unsigned width = 8) { auto altered = bytes; put(altered, at, value, width); reseal(altered); require(!CellularLives::restore(altered, state->world(), state->region(), state->frame(), state->tick()), "forged inherited checkpoint accepted"); }; corrupt(316 + 8 + 504, 0); corrupt(316 + 8 + 504, 100); corrupt(316 + 8 + 504, 999'999); corrupt(history, old_child.life); corrupt(history + 8, 999'999); corrupt(history + 16, 100); corrupt(history + 24, state->tick() + 1); corrupt(308, 0xFFFF'FFFFULL, 4); corrupt(312, 1, 4); corrupt(12, 9, 4); auto flipped = bytes; flipped.at(316) ^= 1; require(!CellularLives::restore(flipped, state->world(), state->region(), state->frame(), state->tick()), "checksum damage accepted"); require(!CellularPopulation::create(restore(*state)) && !CellularMigration::prepare(*state, *state, 100), "unsupported old adapter admitted plans"); CellularBatch old_trace; old_trace.header = {state->world(), state->region(), state->frame(), {}, {}, state->tick(), state->tick() + 1}; old_trace.header.predecessor.fill(1); old_trace.header.predecessor_digest.fill(2); old_trace.ticks.push_back({idle(*state), {}, {}}); const auto encoded = checked(encode_cellular_batch(old_trace), "encode valid old trace"); const auto decoded = checked(decode_cellular_batch(encoded), "decode valid old trace"); const auto replayed = replay_cellular_batch(*state, decoded); if (replayed) throw std::runtime_error("old replay admitted inherited owner"); require(replayed.error() == CellularBatchError::version, "old replay refused for a reason other than version admission"); choose_structure(state, 1'000'001); require(state->offspring_revisions().size() == 2 && parent(*state).identity.genome == 100, "later plan lost earlier history or changed parent"); require(checked(state->archive().organism(old_child.organism), "older child archive").genome == old_child.genome, "later plan rewrote earlier child's birth"); state = restore(*state); bud(state); state = restore(*state); bud(state); require(parent(*state).offspring_genome == 1'000'001 && state->offspring_revisions().size() == 2, "sibling birth reset selected plan"); const auto frozen = parent(*state); for (unsigned tick = 0; tick < 250; ++tick) checked(state->advance_tick_with_mutations(idle(*state, true), {}), "frozen inheritance"); const auto after = parent(*state); require(after.presence == CellularPresence::suspended && after.body == frozen.body && after.reserve == frozen.reserve && after.growth == frozen.growth && after.mutation == frozen.mutation && after.offspring_genome == frozen.offspring_genome, "offline parent progressed its plan or biology"); require(restore(*state)->snapshot() == state->snapshot(), "suspended plan restore"); } void legacy_expression_upgrade() { auto state = opening(7, true, 1); state = checked(state->migration_upgrade_candidate(), "legacy boundary upgrade"); state = checked(state->maturation_upgrade_candidate(), "legacy maturation upgrade"); bool expressed = false; for (unsigned tick = 0; tick < 60'000 && !expressed; ++tick) { const auto before = parent(*state); if (!before.mutation.offer) { checked(state->advance_tick_with_mutations(idle(*state), {}), "legacy earned offer"); continue; } const CellularMutationSelection selection{ 100, before.mutation.event_index, before.mutation.offer_tick, 900'000, before.mutation.offer->candidates.front().operator_id}; const auto result = checked(state->advance_tick_with_mutations(idle(*state), std::span{&selection, 1}), "legacy expression"); expressed = result.mutations.front().result == CellularMutationResult::applied; } require(expressed && parent(*state).identity.genome == 900'000, "legacy mutation was not expressed"); const auto at = state->archive().revisions().back().at.value; const auto archive = state->archive().encode(); state = checked(state->inheritance_upgrade_candidate(), "expressed parent upgrade"); require(state->archive().encode() == archive && parent(*state).offspring_genome == 900'000, "upgrade lost expressed revision"); choose_structure(state, 1'000'000); auto forged = state->snapshot(); put(forged, forged.size() - 8 - 32 + 24, at); reseal(forged); require(!CellularLives::restore(forged, state->world(), state->region(), state->frame(), state->tick()), "plan selected before its expressed baseline existed"); } void death_history() { auto state = family(); for (unsigned tick = 0; tick < 100'000; ++tick) { checked(state->advance_tick_with_mutations(idle(*state, false, false, true), {}), "parent ecological death"); closed(*state); const auto corpse = std::ranges::find(state->corpses(), 100, &CellularOwnedCorpse::life_id); if (corpse != state->corpses().end()) { require(corpse->offspring_genome == 1'000'000 && state->offspring_revisions().size() == 1, "death discarded selected plan history"); require(restore(*state)->snapshot() == state->snapshot(), "dead-plan history restore"); return; } } throw std::runtime_error("finite parent did not die after leaving food"); } 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(), "inheritance checkpoint write"); } } // namespace int main(int argc, char** argv) try { if (argc == 4 && std::string_view{argv[1]} == "--checkpoint") { auto state = family(); save(argv[2], state->snapshot()); continuation(*state); save(argv[3], state->snapshot()); return 0; } if (argc == 4 && std::string_view{argv[1]} == "--resume") { std::ifstream file(argv[2], std::ios::binary); const std::vector bytes{std::istreambuf_iterator{file}, {}}; require(bytes.size() >= 324, "inheritance checkpoint length"); std::uint64_t tick = 0; for (unsigned offset = 0; offset < 8; ++offset) tick |= std::uint64_t{bytes[40 + offset]} << (offset * 8U); auto state = checked(CellularLives::restore(bytes, 207, 1, 1, tick), "new-process plans"); continuation(*state); save(argv[3], state->snapshot()); return 0; } require(argc == 1, "unknown inheritance arguments"); codecs_and_siblings(); legacy_expression_upgrade(); death_history(); return 0; } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; }