#include "evo/server/lifecycle/cellular_migration.hpp" #include "evo/server/lifecycle/cellular_migration_trace.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 plus(Inventory a, const Inventory& b) { const auto maximum = std::numeric_limits::max(); require(b.energy.value <= maximum - a.energy.value, "test energy overflow"); a.energy.value += b.energy.value; for (std::size_t c = 0; c < a.matter.size(); ++c) { require(b.matter[c].value <= maximum - a.matter[c].value, "test matter overflow"); a.matter[c].value += b.matter[c].value; } return a; } void step(CellularLives& owner, bool freeze = true) { auto intents = checked(wildlife_intents(owner, owner.tick() + 1), "migration fixture wildlife"); for (const auto& life : owner.lives()) { if (life.account == LifeAccount{}) continue; CellularLifeIntent intent{life.identity.life, {}}; intent.intent.frozen = freeze; if (!freeze) intent.intent.bud = (owner.tick() + 1 + life.identity.life) % 32 == 0; intents.push_back(intent); } std::ranges::sort(intents, {}, &CellularLifeIntent::life_id); checked(owner.advance_tick(intents), "migration fixture tick"); } Owner funded(bool suspend = true, std::uint32_t founders = 4, std::uint64_t region = 1) { auto owner = opening(1, true, founders, true, region); const auto wildlife = [&] { return std::ranges::any_of(owner->lives(), [](const auto& life) { return life.account == LifeAccount{}; }); }; for (unsigned attempts = 0; attempts < 8192 && !wildlife(); ++attempts) step(*owner, false); require(wildlife(), "migration fixture has no funded wildlife"); if (suspend) for (std::uint64_t i = 0; i <= cellular_linger_ticks; ++i) step(*owner); return owner; } Owner destination(const CellularLives& source, std::uint32_t founders = 0, std::uint64_t world = 0, bool food = false) { const auto endowment = checked(biology::primitive_endowment(biology::primitive_founder_v3), "destination endowment"); Inventory funding; for (std::uint32_t i = 0; i < founders; ++i) { funding = plus(plus(funding, endowment.body), endowment.reserve); funding = plus(funding, {{endowment.construction.value}, {}}); } std::vector sites; sites.reserve(std::max(founders, 1U)); for (std::uint32_t i = 0; i < std::max(founders, 1U); ++i) sites.push_back({1'000 + static_cast(i / 16) * 3'000, 1'000 + static_cast(i % 16) * 3'000}); const auto id = world == 0 ? source.world() : world; const auto region = source.region() == 1 ? 2ULL : 1ULL; auto primitive = checked(PrimitiveLives::create({id, region, source.frame(), 27, funding, sites}), "destination nursery"); const auto area = source.area(); biology::CellularHabitatConfig habitat{id, region, source.frame(), static_cast(area.width_um), static_cast(area.height_um), {}}; habitat.format = biology::cellular_habitat_deposit_format; habitat.next_deposit = 1; if (food) { // Independently funded finite destination food, placed at the incoming // bodies so reproduction tests do not depend on a route to old pools. for (const auto& life : source.lives()) for (std::uint32_t c = 0; c < 5; ++c) { biology::CellularPatch mote; mote.address = {static_cast(life.position.x_um) + 1 + 2 * c, static_cast(life.position.y_um) + 1}; mote.id = habitat.next_deposit++; mote.source = 28; mote.radius_um = biology::cellular_mote_radius_um; mote.kind = biology::CellularDepositKind::mote; if (c == 0) mote.stock.energy.value = 1'000'000'000'000ULL; else mote.stock.matter[c - 1].value = 1'000'000'000'000ULL; habitat.patches.push_back(mote); } std::ranges::sort(habitat.patches, {}, &biology::CellularPatch::address); } auto owner = checked( CellularLives::create( *primitive, checked(biology::CellularHabitat::create(habitat), "destination habitat"), {area, 0, source.ambient_temperature_millikelvin()}, CellularLivesProfile::foraging), "destination owner"); const auto birth = source.tick() - (founders == 0 ? 0 : cellular_linger_ticks + 1); while (owner->tick() < birth) step(*owner); for (std::uint32_t i = 0; i < founders; ++i) { LifeRequest request{}; request.account.front() = 0xF1; request.account[14] = static_cast((i + 1) >> 8U); request.account.back() = static_cast(i + 1); request.action = 1; const auto identity = 1'000'000'000'000ULL + i; owner = checked(owner->birth_candidate( request, {identity, identity, identity, identity, {owner->tick()}}), "destination funded birth"); } while (owner->tick() < source.tick()) step(*owner); require(owner->tick() == source.tick(), "destination cut mismatch"); return owner; } std::uint64_t wildlife_genome(const CellularLives& owner) { const auto found = std::ranges::find_if( owner.lives(), [](const auto& life) { return life.account == LifeAccount{}; }); require(found != owner.lives().end(), "no migration genome"); return found->identity.genome; } void put(std::vector& bytes, std::size_t offset, std::uint64_t value, unsigned width = 8) { for (unsigned i = 0; i < width; ++i) bytes[offset + i] = static_cast(value >> (8U * i)); } void checksum(std::vector& bytes) { std::uint64_t value = 14695981039346656037ULL; for (const auto b : std::span{bytes}.first(bytes.size() - 8)) value = (value ^ b) * 1099511628211ULL; put(bytes, bytes.size() - 8, value); } Owner decode(const CellularLives& trusted, const std::vector& bytes) { return checked(CellularLives::restore(bytes, trusted.world(), trusted.region(), trusted.frame(), trusted.tick()), "migration owner restore"); } template std::array filled(std::uint8_t value) { std::array bytes{}; bytes.fill(value); return bytes; } CellularMigrationRequest request_for(const CellularLives& source, const CellularLives& target) { return {filled<16>(7), {source.region(), filled<16>(1), filled<16>(3), filled<32>(5)}, {target.region(), filled<16>(2), filled<16>(4), filled<32>(6)}, wildlife_genome(source)}; } CellularPopulationExecution continuation(const CellularPopulation& population) { const auto& s = population.state(); const CellularBatchHeader header{s.world(), s.region(), s.frame(), filled<16>(9), filled<32>(10), s.tick(), s.tick() + 64}; auto inputs = checked(unattended_population_inputs(s, header), "transferred interval inputs"); return checked(prepare_population_batch(population, std::move(inputs)), "transferred interval"); } void trace_contract() { auto source = checked(funded()->migration_upgrade_candidate(), "trace source"); auto target = checked(destination(*source, 1)->migration_upgrade_candidate(), "trace target"); const auto before = source->snapshot(), peer_before = target->snapshot(); const auto request = request_for(*source, *target); auto recorded = checked(prepare_migration_trace(*source, *target, request), "record pair"); const auto bytes = checked(encode_migration_trace(recorded.trace), "encode pair"); require(bytes.size() == migration_trace_header_bytes + 8 * recorded.trace.members.size() + 8, "trace length changed"); const auto trace = checked(decode_migration_trace(bytes), "decode pair"); require(trace == recorded.trace, "trace canonical roundtrip"); auto replayed = checked(replay_migration_trace(*source, *target, request, trace), "replay pair"); require(replayed.source->snapshot() == recorded.execution.source->snapshot() && replayed.destination->snapshot() == recorded.execution.destination->snapshot(), "recorded pair replay diverged"); require(!replay_migration_trace(*replayed.source, *replayed.destination, request, trace), "trace applied again to its result"); auto repeated = checked(replay_migration_trace(*source, *target, request, trace), "repeat replay"); require(repeated.source->snapshot() == replayed.source->snapshot() && repeated.destination->snapshot() == replayed.destination->snapshot(), "same immutable inputs replayed differently"); auto altered = trace; ++altered.transferred.energy.value; require(!replay_migration_trace(*source, *target, request, altered), "forged transfer amount"); altered = trace; altered.members.front() += 10'000; require(!replay_migration_trace(*source, *target, request, altered), "forged member set"); auto trusted = request; trusted.destination.predecessor_digest.front() ^= 1; require(!replay_migration_trace(*source, *target, trusted, trace), "wrong destination cut binding"); trusted = request; trusted.source.commit.front() ^= 1; require(!replay_migration_trace(*source, *target, trusted, trace), "wrong result commit binding"); altered = trace; ++altered.tick; require(!replay_migration_trace(*source, *target, request, altered), "wrong trace tick"); altered = trace; std::swap(altered.request.source, altered.request.destination); require(!replay_migration_trace(*source, *target, request, altered), "reversed trace direction"); auto invalid = request; invalid.request = {}; require(!prepare_migration_trace(*source, *target, invalid), "zero transfer request"); invalid = request; invalid.destination.commit = invalid.source.commit; require(!prepare_migration_trace(*source, *target, invalid), "one commit for both heads"); for (std::size_t length = 0; length < bytes.size(); ++length) require(!decode_migration_trace(std::span{bytes}.first(length)), "truncated pair trace"); for (std::size_t offset = 0; offset < bytes.size(); ++offset) { auto corrupt = bytes; corrupt[offset] ^= 1; require(!decode_migration_trace(corrupt), "undetected pair corruption"); } for (const auto offset : {std::size_t{8}, std::size_t{12}, std::size_t{248}, std::size_t{252}}) { auto malformed = bytes; put(malformed, offset, UINT32_MAX, 4); checksum(malformed); require(!decode_migration_trace(malformed), "unbounded/reserved pair header"); } altered = trace; altered.members.clear(); for (std::uint64_t i = 1; i <= biology::max_cellular_tick_lives; ++i) altered.members.push_back(cellular_bud_identity(3, i)); auto maximum = checked(encode_migration_trace(altered), "maximum pair codec"); require(maximum.size() == max_migration_trace_bytes && decode_migration_trace(maximum).has_value(), "bounded maximum trace refused"); require(!replay_migration_trace(*source, *target, request, altered), "synthetic member evidence"); maximum.push_back(0); require(!decode_migration_trace(maximum), "oversized pair trace"); auto population = checked(CellularPopulation::create(decode(*source, before)), "trace cohort"); checked(population->collapse(request.genome), "trace collapse"); auto peer = checked(CellularPopulation::create(decode(*target, peer_before), {false, 1000, 2000}), "trace cohort peer"); auto pop_recorded = checked(prepare_migration_trace(*population, *peer, request), "record cohort pair"); auto pop_trace = checked(decode_migration_trace( checked(encode_migration_trace(pop_recorded.trace), "encode cohort trace")), "decode cohort trace"); auto pop_replayed = checked(replay_migration_trace(*population, *peer, request, pop_trace), "replay cohort pair"); require(pop_replayed.source->snapshot() == pop_recorded.execution.source->snapshot() && pop_replayed.destination->snapshot() == pop_recorded.execution.destination->snapshot() && std::ranges::equal(pop_replayed.destination->members(), population->members()), "cohort pair replay lost representation"); require(!replay_migration_trace(*population, *peer, request, trace) && !replay_migration_trace(*source, *target, request, pop_trace), "trace representation confused"); for (const auto* owner : {pop_replayed.source.get(), pop_replayed.destination.get()}) { const auto bytes_before = owner->snapshot(); const auto ledger_before = owner->state().boundary_ledger(); auto interval = continuation(*owner); require(interval.batch.format_version == 2, "new owner recorded old trace version"); const auto encoded = checked(encode_population_batch(interval.batch), "encode new interval"); const auto decoded = checked(decode_population_batch(encoded), "decode new interval"); auto replay = checked(replay_population_batch(*owner, decoded), "replay new interval"); require(replay->snapshot() == interval.state->snapshot() && replay->state().boundary_ledger() == ledger_before && replay->state().current_total() == owner->state().current_total() && owner->snapshot() == bytes_before, "new interval changed flows/history or failed exact replay"); auto legacy = decoded; legacy.format_version = 1; require(!replay_population_batch(*owner, legacy), "old trace reinterpreted for owner 7"); auto malformed = encoded; malformed[7] = '1'; require(!decode_population_batch(malformed), "new population trace magic/version confusion"); if (!owner->members().empty()) { const auto& s = owner->state(); const CellularBatchHeader h{s.world(), s.region(), s.frame(), filled<16>(9), filled<32>(10), s.tick(), s.tick()}; auto conversion = checked( prepare_population_conversion(*owner, h, {owner->members().front().genome, false}), "new manual trace"); const auto manual = checked(decode_population_batch(checked(encode_population_batch(conversion.batch), "new manual encode")), "new manual decode"); auto materialized = checked(replay_population_batch(*owner, manual), "new manual replay"); require(manual.format_version == 2 && materialized->state().snapshot() == s.snapshot() && materialized->members().empty(), "new conversion changed exact biology"); } } require(source->snapshot() == before && target->snapshot() == peer_before, "trace validation changed its input"); } void write_bytes(const std::string& path, std::span bytes) { std::ofstream out(path, std::ios::binary | std::ios::trunc); out.write(reinterpret_cast(bytes.data()), static_cast(bytes.size())); out.close(); require(out.good(), "restart fixture write"); } std::vector read_bytes(const std::string& path) { std::ifstream in(path, std::ios::binary | std::ios::ate); const auto length = in.tellg(); require(length > 0 && length <= static_cast(max_cellular_population_bytes), "restart fixture bound"); std::vector bytes(static_cast(length)); in.seekg(0); in.read(reinterpret_cast(bytes.data()), static_cast(bytes.size())); require(in.good(), "restart fixture read"); return bytes; } void record_restart(const std::string& prefix) { auto source = checked(funded()->migration_upgrade_candidate(), "restart source"); auto target = checked(destination(*source, 1)->migration_upgrade_candidate(), "restart target"); const auto request = request_for(*source, *target); auto recorded = checked(prepare_migration_trace(*source, *target, request), "restart raw pair"); write_bytes(prefix + ".raw.source", source->snapshot()); write_bytes(prefix + ".raw.destination", target->snapshot()); write_bytes(prefix + ".raw.trace", checked(encode_migration_trace(recorded.trace), "restart raw trace")); write_bytes(prefix + ".raw.source.expected", recorded.execution.source->snapshot()); write_bytes(prefix + ".raw.destination.expected", recorded.execution.destination->snapshot()); auto population = checked(CellularPopulation::create(std::move(source)), "restart population"); auto peer = checked(CellularPopulation::create(std::move(target)), "restart peer"); checked(population->collapse(request.genome), "restart cohort collapse"); auto pop = checked(prepare_migration_trace(*population, *peer, request), "restart population pair"); write_bytes(prefix + ".pop.source", population->snapshot()); write_bytes(prefix + ".pop.destination", peer->snapshot()); write_bytes(prefix + ".pop.trace", checked(encode_migration_trace(pop.trace), "restart pop trace")); write_bytes(prefix + ".pop.source.expected", pop.execution.source->snapshot()); write_bytes(prefix + ".pop.destination.expected", pop.execution.destination->snapshot()); auto interval = continuation(*pop.execution.destination); write_bytes(prefix + ".pop.continuation.trace", checked(encode_population_batch(interval.batch), "restart interval trace")); write_bytes(prefix + ".pop.continuation.expected", interval.state->snapshot()); } void replay_restart(const std::string& prefix) { const auto raw = checked(decode_migration_trace(read_bytes(prefix + ".raw.trace")), "fresh raw trace"); auto source = checked(CellularLives::restore(read_bytes(prefix + ".raw.source"), 201, 1, 1, raw.tick), "fresh source"); auto target = checked( CellularLives::restore(read_bytes(prefix + ".raw.destination"), 201, 2, 1, raw.tick), "fresh target"); const auto trusted = request_for(*source, *target); // Fixed fixture IDs, not an inferred live grant. auto replayed = checked(replay_migration_trace(*source, *target, trusted, raw), "fresh raw replay"); write_bytes(prefix + ".raw.source.resumed", replayed.source->snapshot()); write_bytes(prefix + ".raw.destination.resumed", replayed.destination->snapshot()); const auto pop = checked(decode_migration_trace(read_bytes(prefix + ".pop.trace")), "fresh pop trace"); auto population = checked( CellularPopulation::restore(read_bytes(prefix + ".pop.source"), 201, 1, 1, pop.tick), "fresh cohort"); auto peer = checked( CellularPopulation::restore(read_bytes(prefix + ".pop.destination"), 201, 2, 1, pop.tick), "fresh peer"); auto pop_replayed = checked(replay_migration_trace(*population, *peer, trusted, pop), "fresh population replay"); write_bytes(prefix + ".pop.source.resumed", pop_replayed.source->snapshot()); write_bytes(prefix + ".pop.destination.resumed", pop_replayed.destination->snapshot()); const auto interval = checked(decode_population_batch(read_bytes(prefix + ".pop.continuation.trace")), "fresh interval trace"); auto continued = checked(replay_population_batch(*pop_replayed.destination, interval), "fresh transferred continuation"); write_bytes(prefix + ".pop.continuation.resumed", continued->snapshot()); } void upgrade_and_move() { auto original = funded(); auto other = destination(*original, 1); const auto original_bytes = original->snapshot(); auto source = checked(original->migration_upgrade_candidate(), "source explicit upgrade"); auto target = checked(other->migration_upgrade_candidate(), "destination explicit upgrade"); require(source->format_version() == 7 && source->rules_version() == 8 && source->snapshot().size() == original_bytes.size() + 88, "upgrade rewrote biological format"); require(std::ranges::equal(source->lives(), original->lives()) && source->archive().encode() == original->archive().encode() && source->habitat().snapshot() == original->habitat().snapshot() && source->opening_total() == original->opening_total() && source->boundary_ledger() == CellularBoundaryLedger{} && original->snapshot() == original_bytes, "upgrade changed biology/funding"); const auto source_before = source->snapshot(), target_before = target->snapshot(); const auto total = plus(source->current_total(), target->current_total()); const auto genome = wildlife_genome(*source); auto moved = checked(CellularMigration::prepare(*source, *target, genome), "exact migration"); require(!moved.members.empty() && moved.source->tick() == source->tick() && moved.destination->tick() == target->tick(), "migration aged biology"); require(source->snapshot() == source_before && target->snapshot() == target_before, "migration mutated an input"); require(plus(moved.source->current_total(), moved.destination->current_total()) == total && moved.source->opening_total() == source->opening_total() && moved.destination->opening_total() == target->opening_total(), "migration lost stock or rebased opening"); require(moved.source->archive().encode() == source->archive().encode() && moved.source->boundary_ledger().nonresident_organisms == moved.members, "departure erased history or recorded death"); for (const auto id : moved.members) { const auto before = std::ranges::find(source->lives(), id, [](const auto& life) { return life.identity.life; }); const auto after = std::ranges::find(moved.destination->lives(), id, [](const auto& life) { return life.identity.life; }); require(before != source->lives().end() && after != moved.destination->lives().end() && *before == *after, "member was relabeled/healed/resampled"); require(moved.destination->archive().organism(id) == source->archive().organism(id), "birth history changed"); } require(moved.destination->next_bud_sequence() == target->next_bud_sequence(), "import consumed a local bud identity"); auto source_after = decode(*moved.source, moved.source->snapshot()); auto target_after = decode(*moved.destination, moved.destination->snapshot()); const auto members = moved.members; for (unsigned turn = 0; turn < 100; ++turn) { const bool back = turn % 2 == 0; auto next = checked(CellularMigration::prepare(back ? *target_after : *source_after, back ? *source_after : *target_after, genome), "migration return trip"); if (back) { source_after = std::move(next.destination); target_after = std::move(next.source); } else { source_after = std::move(next.source); target_after = std::move(next.destination); } source_after = decode(*source_after, source_after->snapshot()); target_after = decode(*target_after, target_after->snapshot()); require(plus(source_after->current_total(), target_after->current_total()) == total, "return trip duplicated inventory"); } // 101 total moves: the group remains at the destination with every body exact. require(source_after->boundary_ledger().nonresident_organisms == members, "nonresident history changed on oscillation"); step(*source_after); step(*target_after); require(plus(source_after->current_total(), target_after->current_total()) == total, "continued biology broke boundary accounting"); bool died = false; for (unsigned i = 0; i < 8192 && !died; ++i) { step(*target_after); died = std::ranges::any_of(members, [&](auto id) { return !checked(target_after->archive().organism(id), "imported organism").alive; }); } require(died, "imported wildlife failed to die without food"); target_after = decode(*target_after, target_after->snapshot()); require(target_after->current_total() == moved.destination->current_total(), "death/decay spent boundary history as stock"); std::cout << "migration members=" << members.size() << " source_bytes=" << source_before.size() << " target_bytes=" << target_before.size() << " oscillations=100\n"; } void population_envelope() { auto population = checked(CellularPopulation::create(funded()), "population before migration"); const auto genome = wildlife_genome(population->state()); checked(population->collapse(genome), "cohort before upgrade"); const auto old = population->snapshot(); auto next = checked(population->migration_upgrade_candidate(), "population explicit upgrade"); require(next->format_version() == 2 && population->format_version() == 1 && population->snapshot() == old && std::ranges::equal(next->members(), population->members()) && next->conversion_sequence() == population->conversion_sequence() && next->policy() == population->policy(), "population upgrade lost tags/policy or changed its input"); const auto bytes = next->snapshot(); next = checked(CellularPopulation::restore(bytes, next->state().world(), next->state().region(), next->state().frame(), next->state().tick()), "population-2 restore"); require(next->snapshot() == bytes, "population-2 roundtrip changed"); auto bad = bytes; bad[7] = '1'; put(bad, 8, 1, 4); checksum(bad); require(!CellularPopulation::restore(bad, next->state().world(), next->state().region(), next->state().frame(), next->state().tick()), "new owner admitted as historical envelope 1"); CellularPopulationBatch old_trace; require(!execute_population_batch(*next, old_trace, true), "old durable trace admitted new owner tuple"); auto peer = checked( CellularPopulation::create(checked( destination(next->state())->migration_upgrade_candidate(), "cohort peer upgrade")), "cohort peer"); const auto peer_before = peer->snapshot(); auto migrated = checked(CellularMigration::prepare(*next, *peer, genome), "cohort transfer"); require(migrated.source->members().empty() && std::ranges::equal(migrated.destination->members(), next->members()) && migrated.source->conversion_sequence() == next->conversion_sequence() && migrated.destination->conversion_sequence() == peer->conversion_sequence() && migrated.source->policy() == next->policy() && migrated.destination->policy() == peer->policy() && next->snapshot() == bytes && peer->snapshot() == peer_before, "migration changed representation, policy, sequence or input"); auto restored = checked(CellularPopulation::restore(migrated.destination->snapshot(), peer->state().world(), peer->state().region(), peer->state().frame(), peer->state().tick()), "migrated cohort restore"); require(std::ranges::equal(restored->members(), next->members()), "cohort tags lost on restore"); checked(next->materialize(genome), "new envelope materialization"); std::vector controlled; for (const auto& life : next->state().lives()) if (life.account != LifeAccount{}) { CellularLifeIntent frozen{life.identity.life, {}}; frozen.intent.frozen = true; controlled.push_back(frozen); } checked(next->advance_tick(controlled), "new envelope continued biology"); } void overlap_refusal() { auto raw = funded(false); for (unsigned i = 0; i < 128; ++i) step(*raw, false); for (std::uint64_t i = 0; i <= cellular_linger_ticks; ++i) step(*raw); auto source = checked(raw->migration_upgrade_candidate(), "overlap source upgrade"); auto target = checked(destination(*source)->migration_upgrade_candidate(), "overlap peer"); const auto first = wildlife_genome(*source); auto initial = checked(CellularMigration::prepare(*source, *target, first), "overlap first group"); source = std::move(initial.source); target = std::move(initial.destination); const auto second = wildlife_genome(*source); require(second != first, "overlap fixture lacks an unrelated group"); const auto occupied = std::ranges::find_if( target->lives(), [](const auto& life) { return life.account == LifeAccount{}; }); require(occupied != target->lives().end(), "missing occupied destination"); auto bytes = source->snapshot(); for (std::size_t i = 0; i < source->lives().size(); ++i) if (source->lives()[i].account == LifeAccount{} && source->lives()[i].identity.genome == second) { // This fixture has four founding sites; v7 header 308, life stride 504. const auto record = 308 + 4 * 8 + i * 504; put(bytes, record + 72, occupied->position.x_um); put(bytes, record + 80, occupied->position.y_um); } checksum(bytes); source = decode(*source, bytes); // Legal resident coordinates; transfer must test the peer. const auto other = target->snapshot(); const auto result = CellularMigration::prepare(*source, *target, second); require(!result && result.error() == CellularMigrationError::placement && source->snapshot() == bytes && target->snapshot() == other, "overlapping transfer displaced/partially moved cells"); } void refusal_and_codec() { auto old = funded(); auto raw_target = destination(*old); auto source = checked(old->migration_upgrade_candidate(), "codec source upgrade"); auto target = checked(raw_target->migration_upgrade_candidate(), "codec target upgrade"); const auto genome = wildlife_genome(*source); const auto before = source->snapshot(), target_before = target->snapshot(); require(!CellularMigration::prepare(*old, *target, genome), "legacy owner silently migrated"); require(!CellularMigration::prepare(*source, *source, genome), "same owner migrated to itself"); require(!CellularMigration::prepare(*source, *target, 0) && !CellularMigration::prepare(*source, *target, 999999), "absent group migrated"); auto late = decode(*target, target_before); step(*late); require(!CellularMigration::prepare(*source, *late, genome), "different biological cuts migrated"); auto alien = checked(destination(*source, 0, source->world() + 1)->migration_upgrade_candidate(), "alien upgrade"); require(!CellularMigration::prepare(*source, *alien, genome), "world boundary relabeled"); auto active = funded(false); auto active_target = destination(*active); active = checked(active->migration_upgrade_candidate(), "active representation upgrade"); active_target = checked(active_target->migration_upgrade_candidate(), "active peer upgrade"); require(!CellularMigration::prepare(*active, *active_target, wildlife_genome(*active)), "vulnerable controlled life entered transfer"); auto bad = before; put(bad, 220, 1); checksum(bad); require(!CellularLives::restore(bad, source->world(), source->region(), source->frame(), source->tick()), "unfunded boundary credit admitted"); bad = before; put(bad, 220 + 8, 1); put(bad, 260 + 16, 1); checksum(bad); require(!CellularLives::restore(bad, source->world(), source->region(), source->frame(), source->tick()), "different chemical channels canceled"); const auto execution = checked(CellularMigration::prepare(*source, *target, genome), "overflow amount reference"); std::array amounts{ execution.transferred.energy.value, execution.transferred.matter[0].value, execution.transferred.matter[1].value, execution.transferred.matter[2].value, execution.transferred.matter[3].value}; const auto channel = static_cast( std::ranges::find_if(amounts, [](auto value) { return value != 0; }) - amounts.begin()); require(channel < amounts.size(), "zero-stock organism migrated"); bad = before; put(bad, 220 + channel * 8, UINT64_MAX); put(bad, 260 + channel * 8, UINT64_MAX); checksum(bad); auto exhausted = decode(*source, bad); // Balanced huge flows never become spendable stock. auto refused = CellularMigration::prepare(*exhausted, *target, genome); require(!refused && refused.error() == CellularMigrationError::overflow, "outflow wrapped"); bad = target_before; put(bad, 220 + channel * 8, UINT64_MAX); put(bad, 260 + channel * 8, UINT64_MAX); checksum(bad); auto full = decode(*target, bad); refused = CellularMigration::prepare(*source, *full, genome); require(!refused && refused.error() == CellularMigrationError::overflow, "inflow wrapped"); for (const auto offset : {std::size_t{300}, std::size_t{304}}) { bad = before; put(bad, offset, UINT32_MAX, 4); checksum(bad); require(!CellularLives::restore(bad, source->world(), source->region(), source->frame(), source->tick()), "unbounded/reserved migration header admitted"); } const auto stride = std::max(std::size_t{1}, before.size() / 32); for (std::size_t length = 0; length < before.size(); length += stride) require(!CellularLives::restore(std::span{before}.first(length), source->world(), source->region(), source->frame(), source->tick()), "truncated migration owner admitted"); require(source->snapshot() == before && target->snapshot() == target_before, "rejection modified a peer"); } void capacity() { auto raw = funded(true, 1); auto crowded = destination(*raw, 256); auto source = checked(raw->migration_upgrade_candidate(), "capacity source upgrade"); auto target = checked(crowded->migration_upgrade_candidate(), "capacity target upgrade"); const auto before = source->snapshot(), other = target->snapshot(); const auto result = CellularMigration::prepare(*source, *target, wildlife_genome(*source)); require(!result && result.error() == CellularMigrationError::capacity && source->snapshot() == before && target->snapshot() == other, "capacity pressure culled stock or partially moved a group"); } void local_budding_among_imports() { auto raw = funded(true, 4, 2); auto source = checked(raw->migration_upgrade_candidate(), "higher namespace source"); auto target = checked(destination(*source, 1, 0, true)->migration_upgrade_candidate(), "lower namespace funded destination"); auto moved = checked(CellularMigration::prepare(*source, *target, wildlife_genome(*source)), "higher-to-lower namespace migration"); target = std::move(moved.destination); const auto opening = target->current_total(); const auto next = target->next_bud_sequence(); const auto founder = std::ranges::find_if(target->lives(), [](const auto& life) { return life.account != LifeAccount{}; })->identity.life; bool local_parent = false; for (unsigned i = 0; i < 8192 && target->next_bud_sequence() == next; ++i) { auto intents = checked(wildlife_intents(*target, target->tick() + 1), "imported intents"); CellularLifeIntent controlled{founder, {}}; controlled.intent.bud = true; intents.push_back(controlled); std::ranges::sort(intents, {}, &CellularLifeIntent::life_id); checked(target->advance_tick(intents), "local real budding among imports"); for (const auto& life : target->lives()) if (life.identity.life >= cellular_bud_identity(1, next) && life.identity.life < cellular_bud_identity(2, 1) && life.parent_life == founder) local_parent = true; } require(local_parent && target->next_bud_sequence() > next && target->current_total() == opening, "local reproduction among imported IDs failed or spent boundary flows"); target = decode(*target, target->snapshot()); for (unsigned i = 0; i < 64; ++i) step(*target); require(std::ranges::is_sorted(target->lives(), {}, [](const auto& life) { return life.identity.life; }), "local children displaced foreign namespace ordering"); } } // namespace int main(int argc, char** argv) try { if (argc == 3) { const std::string_view mode{argv[1]}; if (mode == "--record") record_restart(argv[2]); else if (mode == "--replay") replay_restart(argv[2]); else throw std::runtime_error("unknown migration fixture mode"); return 0; } require(argc == 1, "migration test arguments"); upgrade_and_move(); refusal_and_codec(); capacity(); population_envelope(); overlap_refusal(); local_budding_among_imports(); trace_contract(); return 0; } catch (const std::exception& error) { std::cerr << error.what() << '\n'; return 1; }