#include "evo/server/lifecycle/cellular_batch.hpp" #include "fixtures/cellular_ecology.hpp" #include #include // Rules 13 owner (Cellular_Revamp_Build_Plan.md phase 1, ADR-0179): the format 10 -> 11 upgrade, // founder 6 births, the declared revamp rules and exact restore and replay. namespace { using namespace evo; using namespace evo::server; using namespace evo::fixture; CellularLiquidRules liquid() { return {.currents = {5, 48'000, 48'000, 250, 2, 4, 3'000, 6'000, 60, 300}, .founder = {.thrust_q10 = 20'480, .reverse_thrust_q10 = 20'480, .forward_drag_ppm = 95'163, .lateral_drag_ppm = 95'163, .turn = 3'276}, .deposits = {.drift_radius_um = 200, .max_drift_q10 = 100 * 1'024, .drift_decay_ppm = 95'163, .merged_cap = 8, .bubble_radius_um = biology::cellular_mote_radius_um}}; } // The trial revamp rules (deploy/kubernetes/region/dev/tuning/cellular-revamp.tuning). CellularRevampRules revamp(std::uint32_t essential = 250) { return {.body = {.essential_upkeep_per_thousand = essential}, .burn_pj_per_kfg = {2'000, 2'000, 2'000, 2'000}, .step = {.membrane_collection_per_mille_per_point = 2, .engulf_ratio_milli = 1'500, .engulf_reserve_percent = 100, .kill_bubble_per_thousand = 500}, .drip_interval_ticks = 20, .drip_bubble_pj = 20'000'000, .drip_bubble_fg = 1'000'000, .reserve_load_per_thousand = 1'000}; } // Reserve matter slows thrust and turning and raises thrust costs in proportion to the body. void carried_matter_is_load() { const biology::CellularHandling base{.thrust_q10 = 20'480, .reverse_thrust_q10 = 10'240, .forward_drag_ppm = 95'163, .lateral_drag_ppm = 95'163, .turn = 3'276, .thrust_cost = {1'000}, .reverse_cost = {500}}; require(cellular_loaded_handling(base, 8'000'000, 0, revamp()) == base, "an empty reserve changed the handling"); const auto heavy = cellular_loaded_handling(base, 8'000'000, 8'000'000, revamp()); require(heavy.thrust_q10 == 10'240 && heavy.reverse_thrust_q10 == 5'120 && heavy.turn == 1'638 && heavy.thrust_cost.value == 2'000 && heavy.reverse_cost.value == 1'000 && heavy.forward_drag_ppm == base.forward_drag_ppm, "carrying the body's mass again did not halve thrust and double its cost"); auto none = revamp(); none.reserve_load_per_thousand = 0; require(cellular_loaded_handling(base, 8'000'000, 8'000'000, none) == base, "a zero load share still loaded the cell"); } LifeAccount account(std::uint8_t last) { LifeAccount value{}; value.back() = last; return value; } // A rules-13 region with a founder 5 born before the upgrade and a founder 6 born after it. Owner revamp_region(std::uint32_t essential = 250) { auto state = opening(7, true, 2, false); // Funds two founders. checked(state->advance_tick({}), "genesis tick"); state = checked(state->migration_upgrade_candidate(), "boundary upgrade"); state = checked(state->maturation_upgrade_candidate(), "maturation upgrade"); state = checked(state->inheritance_upgrade_candidate(), "inheritance upgrade"); state = checked(state->photosynthesis_upgrade_candidate({0, 1, {0}}), "light upgrade"); require(!state->revamp_upgrade_candidate(revamp()), "revamp upgrade skipped rules 12"); state = checked(state->liquid_upgrade_candidate(liquid()), "liquid upgrade"); state = checked(state->birth_candidate({account(1), 1}, {100, 100, 100, 100, {state->tick()}}), "founder 5 birth"); require(!state->revamp_upgrade_candidate(revamp(1'001)), "an invalid essential share accepted"); state = checked(state->revamp_upgrade_candidate(revamp(essential)), "revamp upgrade"); require(state->format_version() == cellular_lives_revamp_format && state->rules_version() == cellular_lives_revamp_rules && state->revamp_rules() == revamp(essential) && state->liquid_rules() == liquid() && state->flow_field() != nullptr && !state->revamp_upgrade_candidate(revamp()), "revamp upgrade repeated, wrong versions or lost rules"); state = checked(state->birth_candidate({account(2), 1}, {101, 101, 101, 101, {state->tick()}}), "founder 6 birth"); const auto old = state->archive().find(100); const auto fresh = state->archive().find(101); require(old && old->schema_version() == biology::genome_schema_v6, "the founder 5 life lost its genome in the upgrade"); require(fresh && fresh->schema_version() == biology::genome_schema_v7 && fresh->development_version() == 7 && fresh->cellular_traits() == biology::founder_cellular_traits, "a rules-13 founder is not founder 6"); return state; } std::vector swim(const CellularLives& state, biology::CellularIntent movement, std::uint8_t exclude = 0) { std::vector result; for (const auto& life : state.lives()) { CellularLifeIntent input{life.identity.life, {}}; input.intent.movement = movement; input.intent.exclude = exclude; result.push_back(input); } return result; } void closed(const CellularLives& state) { const auto& ledger = state.boundary_ledger(); auto expected = state.opening_total(); expected.energy.value += ledger.incoming.energy.value - ledger.outgoing.energy.value; for (std::size_t channel = 0; channel < expected.matter.size(); ++channel) expected.matter[channel].value += ledger.incoming.matter[channel].value - ledger.outgoing.matter[channel].value; require(state.current_total() == expected, "revamp region broke closure"); } void upgrade_and_restore() { const auto state = revamp_region(400); const auto copy = restore(*state); require(copy->snapshot() == state->snapshot() && copy->revamp_rules() == revamp(400) && copy->liquid_rules() == liquid(), "format 11 did not restore exactly"); // A tampered rules block is refused rather than read as another share. auto bytes = state->snapshot(); require(bytes.size() > 8, "empty snapshot"); bytes[8] = 10; // Format field: format 10 bytes with the format 11 magic. require(!CellularLives::restore(bytes, state->world(), state->region(), state->frame(), state->tick()), "a format 11 checkpoint restored as format 10"); } // Both generations live and pay their way in one region, and restore continues exactly. void mixed_generations_tick_and_continue() { auto continuous = revamp_region(); for (int tick = 0; tick < 30; ++tick) checked(continuous->advance_tick(swim(*continuous, {600, 800})), "continuous tick"); require(continuous->lives().size() == 2, "a life died in thirty ticks"); closed(*continuous); auto resumed = restore(*continuous); for (int tick = 0; tick < 30; ++tick) { checked(continuous->advance_tick(swim(*continuous, {-700, 300})), "continuous tick"); checked(resumed->advance_tick(swim(*resumed, {-700, 300})), "resumed tick"); } require(continuous->snapshot() == resumed->snapshot(), "restore diverged from a live run"); closed(*continuous); } // The spent pool drips back one bubble per non-empty channel on every interval tick, at // seeded spots, without losing or creating anything; a full habitat keeps it waiting. void the_spent_pool_drips_back() { const auto rules = revamp(); biology::CellularHabitatConfig config{7, 9, 11, 48'000, 48'000, {}}; config.format = biology::cellular_habitat_liquid_format; config.next_deposit = 1; accounting::Inventory spent{{50'000'000}, {{{2'500'000}, {0}, {300}, {1'000'000}}}}; auto off = config; auto kept = spent; require(cellular_drip_spent(off, kept, rules, 1, 2, 19, 5) && off == config && kept == spent, "the pool dripped between intervals"); auto once = config; auto left = spent; require(cellular_drip_spent(once, left, rules, 1, 2, 20, 5), "drip refused"); require(once.patches.size() == 4 && once.next_deposit == 5, "a drip is one bubble per non-empty channel"); accounting::Inventory dripped{}; for (const auto& bubble : once.patches) { require(bubble.kind == biology::CellularDepositKind::mote && bubble.merged == 1 && bubble.source == 5 && bubble.address.x_um < 48'000 && bubble.address.y_um < 48'000, "a drip bubble is malformed"); dripped.energy.value += bubble.stock.energy.value; for (std::size_t c = 0; c < 4; ++c) dripped.matter[c].value += bubble.stock.matter[c].value; } require(dripped == accounting::Inventory{{20'000'000}, {{{1'000'000}, {0}, {300}, {1'000'000}}}} && left == accounting::Inventory{{30'000'000}, {{{1'500'000}, {0}, {0}, {0}}}}, "the drip did not move the capped amounts exactly"); auto again = config; auto same = spent; require(cellular_drip_spent(again, same, rules, 1, 2, 20, 5) && again == once, "the drip is not deterministic"); auto full = config; for (std::uint32_t i = 0; i < biology::max_cellular_liquid_deposits; ++i) { biology::CellularPatch filler{{i % 48'000, i / 48'000}, 5, {{1}, {}}, ++full.next_deposit, biology::cellular_mote_radius_um, biology::CellularDepositKind::mote}; filler.merged = 1; full.patches.push_back(filler); } const auto packed = full; auto waiting = spent; require(cellular_drip_spent(full, waiting, rules, 1, 2, 20, 5) && full == packed && waiting == spent, "a full habitat took a drip"); } // Exclusions belong to owned lives under rules 13 and travel in the trace. void exclusions_are_checked_and_replayed() { auto state = revamp_region(); require(state->advance_tick(swim(*state, {300, 0}, 0b0101)).has_value(), "an owned life's exclusions were refused"); require(!state->advance_tick(swim(*state, {}, 0x10)).has_value(), "an exclusion outside the four matter channels was accepted"); auto older = opening(7, true, 2, false); checked(older->advance_tick({}), "genesis tick"); older = checked(older->migration_upgrade_candidate(), "boundary upgrade"); older = checked(older->maturation_upgrade_candidate(), "maturation upgrade"); older = checked(older->inheritance_upgrade_candidate(), "inheritance upgrade"); older = checked(older->photosynthesis_upgrade_candidate({0, 1, {0}}), "light upgrade"); older = checked(older->liquid_upgrade_candidate(liquid()), "liquid upgrade"); older = checked(older->birth_candidate({account(1), 1}, {100, 100, 100, 100, {older->tick()}}), "founder 5 birth"); require(!older->advance_tick(swim(*older, {}, 0b0001)).has_value(), "a rules-12 owner accepted exclusions"); } // A rules-13 region runs long enough for founders to spend their endowment, keeping closure, // and restores with its rules and spent pool exactly. void a_long_run_keeps_closure_and_restores() { auto state = revamp_region(); for (int tick = 0; tick < 700; ++tick) { const auto x = static_cast((tick / 50) % 2 == 0 ? 900 : -900); checked(state->advance_tick(swim(*state, {x, 400}, 0b0010)), "long-run tick"); } closed(*state); const auto copy = restore(*state); require(copy->snapshot() == state->snapshot() && copy->revamp_rules() == revamp(), "format 11 did not restore the rules and spent pool exactly"); } // Rules 13 offers: active play earns them, behaviour moves affinity, and a choice edits the // plan (never the expressed body), pays and reclaims its cost, or keeps the offer when // unaffordable; a decline clears it; a pending offer restores exactly. const CellularOwnedLife& life_of(const CellularLives& state, std::uint64_t id) { const auto found = std::ranges::find( state.lives(), id, [](const CellularOwnedLife& l) { return l.identity.life; }); require(found != state.lives().end(), "life missing"); return *found; } Owner offering_region(std::uint64_t cost_pj) { auto rules = revamp(); rules.offer_interval_ticks = 40; for (auto& m : rules.mutations) { m.cost.energy.value = cost_pj; m.cost_scale_percent = 100; } auto fresh = opening(7, true, 2, false); checked(fresh->advance_tick({}), "genesis tick"); fresh = checked(fresh->migration_upgrade_candidate(), "boundary upgrade"); fresh = checked(fresh->maturation_upgrade_candidate(), "maturation upgrade"); fresh = checked(fresh->inheritance_upgrade_candidate(), "inheritance upgrade"); fresh = checked(fresh->photosynthesis_upgrade_candidate({0, 1, {0}}), "light upgrade"); fresh = checked(fresh->liquid_upgrade_candidate(liquid()), "liquid upgrade"); fresh = checked(fresh->birth_candidate({account(1), 1}, {100, 100, 100, 100, {fresh->tick()}}), "founder 5 birth"); fresh = checked(fresh->revamp_upgrade_candidate(rules), "revamp upgrade"); fresh = checked(fresh->birth_candidate({account(2), 1}, {101, 101, 101, 101, {fresh->tick()}}), "founder 6 birth"); return fresh; } void offers_come_from_active_play() { auto state = offering_region(0); for (int tick = 0; tick < 40; ++tick) checked(state->advance_tick(swim(*state, {1'000, 0})), "swim tick"); const auto& founder6 = life_of(*state, 101).revamp; require(founder6.offer.count == 3 && founder6.offer.event_index == 1 && founder6.events == 1 && founder6.active_ticks == 0, "forty ticks of active play did not earn a founder 6 an offer"); for (std::size_t k = 0; k < 3; ++k) require(biology::cellular_mutation(founder6.offer.ids.at(k)) != nullptr && founder6.offer.chances.at(k) >= 10 && founder6.offer.chances.at(k) <= 700'000, "an offered mutation is unknown or its chance out of bounds"); // A founder 5 plan (no traits) has no catalog: its life counts but is never offered. require(life_of(*state, 100).revamp.offer.count == 0, "a founder 5 plan was offered"); // Thrust moved affinity: forty ticks is short of the ten seconds Strong Propulsion counts. require(founder6.affinity.score.at(0) == 0, "affinity moved too early"); const auto copy = restore(*state); require(copy->snapshot() == state->snapshot() && life_of(*copy, 101).revamp == founder6, "a pending offer did not restore exactly"); } std::vector choose(const CellularLives& state, std::uint64_t life_id, std::uint16_t id, std::uint64_t successor) { const auto& offer = life_of(state, life_id).revamp.offer; return {{life_id, offer.event_index, offer.offer_tick, successor, id}}; } void a_choice_edits_the_plan_and_pays() { auto state = offering_region(1'000); for (int tick = 0; tick < 40; ++tick) checked(state->advance_tick(swim(*state, {1'000, 0})), "swim tick"); const auto before = life_of(*state, 101); const auto id = before.revamp.offer.ids.at(0); const auto deposits = state->habitat().patches().size(); const auto execution = checked( state->advance_tick_with_mutations(swim(*state, {}), choose(*state, 101, id, 9'000)), "choice tick"); require(execution.mutations.size() == 1 && execution.mutations.front().result == CellularMutationResult::offspring_plan_applied, "the choice was not applied to the plan"); const auto& after = life_of(*state, 101); require(after.offspring_genome == 9'000 && after.identity.genome == before.identity.genome && after.revamp.offer.count == 0, "the choice changed the expressed body or left the offer"); const auto plan = state->archive().find(9'000); require(plan && plan->parents().size() == 1 && plan->parents()[0] == before.identity.genome, "the successor plan is not the founder's child"); // 1,000 pJ paid (upkeep and swimming aside) returned as one new bubble. require(state->habitat().patches().size() >= deposits + 1, "the paid cost was not reclaimed"); closed(*state); } void unaffordable_choices_wait_and_declines_clear() { auto state = offering_region(1'000'000'000'000); for (int tick = 0; tick < 40; ++tick) checked(state->advance_tick(swim(*state, {1'000, 0})), "swim tick"); const auto offer = life_of(*state, 101).revamp.offer; const auto execution = checked(state->advance_tick_with_mutations(swim(*state, {}), choose(*state, 101, offer.ids.at(1), 9'001)), "unaffordable choice tick"); require(execution.mutations.front().result == CellularMutationResult::insufficient_funding && life_of(*state, 101).revamp.offer == offer && !state->archive().find(9'001), "an unaffordable choice did not leave the offer waiting"); const auto declined = checked(state->advance_tick_with_mutations(swim(*state, {}), choose(*state, 101, 0, 9'002)), "decline tick"); require(declined.mutations.front().result == CellularMutationResult::declined && life_of(*state, 101).revamp.offer.count == 0 && life_of(*state, 101).offspring_genome == life_of(*state, 101).identity.genome, "a decline did not clear the offer"); // A choice naming something not offered fails the tick. for (int tick = 0; tick < 40; ++tick) checked(state->advance_tick(swim(*state, {1'000, 0})), "swim tick"); const auto next = life_of(*state, 101).revamp.offer; std::uint16_t absent = 1; while (std::ranges::find(next.ids, absent) != next.ids.end()) ++absent; require( !state->advance_tick_with_mutations(swim(*state, {}), choose(*state, 101, absent, 9'003)), "a choice outside the offer was accepted"); closed(*state); } void thrust_moves_affinity() { auto state = offering_region(0); for (int tick = 0; tick < 200; ++tick) checked(state->advance_tick(swim(*state, {1'000, 0})), "swim tick"); const auto& scores = life_of(*state, 101).revamp.affinity.score; // Ten seconds of thrust: Strong Propulsion up, Light Propulsion down (catalog ids 1 and 7). require(scores.at(0) == 100 && scores.at(6) == -100, "ten seconds of thrust did not move propulsion affinity"); } // A recorded trace replays to the exact live result under rules 13. void trace_replays() { auto live = revamp_region(); const auto base = restore(*live); CellularBatch batch; batch.format_version = cellular_batch_inheritance_format; batch.header = {live->world(), live->region(), live->frame(), {}, {}, live->tick(), 0}; batch.header.predecessor.fill(1); batch.header.predecessor_digest.fill(2); for (int tick = 0; tick < 24; ++tick) { const auto intents = swim(*live, {static_cast(tick < 12 ? 1'000 : -400), 0}, static_cast(tick % 16)); checked(live->advance_tick(intents), "recorded tick"); batch.ticks.push_back({intents, {}, {}}); } batch.header.end_tick = live->tick(); const auto bytes = checked(encode_cellular_batch(batch), "trace encodes"); const auto decoded = checked(decode_cellular_batch(bytes), "trace decodes"); const auto replayed = checked(replay_cellular_batch(*base, decoded), "trace replays"); require(replayed->snapshot() == live->snapshot(), "replay diverged from the live run"); require(decoded.ticks.back().lives.front().intent.exclude == 23 % 16, "exclusions did not survive the trace"); } } // namespace int main() try { upgrade_and_restore(); mixed_generations_tick_and_continue(); the_spent_pool_drips_back(); carried_matter_is_load(); exclusions_are_checked_and_replayed(); a_long_run_keeps_closure_and_restores(); offers_come_from_active_play(); a_choice_edits_the_plan_and_pays(); unaffordable_choices_wait_and_declines_clear(); thrust_moves_affinity(); trace_replays(); std::puts("PASS rules 13 owner: upgrade, founder 6, restore and replay"); return 0; } catch (...) { std::puts("FAIL rules 13 owner"); return 1; }