#include "evo/persistence/region_checkpoint.hpp" #include "evo/server/lifecycle/cellular_population_batch.hpp" #include "fixtures/cellular_ecology.hpp" namespace { using namespace evo; using namespace evo::server; using namespace evo::persistence; using namespace evo::fixture; template std::array filled(std::uint8_t byte) { std::array out{}; out.fill(byte); return out; } CellularBatchHeader header(const CellularPopulation& state, std::uint64_t ticks) { const auto& s = state.state(); return {s.world(), s.region(), s.frame(), filled<16>(1), filled<32>(2), s.tick(), s.tick() + ticks}; } Owner interval_source() { auto source = cellular_descendants(); const auto stop = source->tick() + cellular_linger_ticks + 1; while (source->tick() < stop) { const CellularBatchHeader h{source->world(), source->region(), source->frame(), filled<16>(1), filled<32>(2), source->tick(), std::min(stop, source->tick() + 64)}; auto inputs = checked(unattended_population_inputs(*source, h), "suspension prefix inputs"); source = checked(replay_cellular_batch(*source, inputs), "suspension prefix replay"); } require(population_interval_safe(*source), "controlled life did not finish lingering"); return source; } void lingering_is_not_new_population_work() { auto active = checked(CellularPopulation::create(cellular_descendants()), "legacy active source"); require(!population_interval_safe(active->state()), "active controlled source admitted"); auto biology = checked(unattended_population_inputs(active->state(), header(*active, 1)), "legacy frozen tick"); require(!prepare_population_batch(*active, biology), "new interval bypassed vulnerable lingering"); auto biological = checked(replay_cellular_batch(active->state(), biology), "legacy recorded biology"); require(!population_interval_safe(*biological), "lingering controlled source admitted"); auto expected = checked(CellularPopulation::create(std::move(biological)), "legacy population result"); // Existing profile-5 records retain their meaning. They can be verified and // materialized to profile 4; new interval preparation rejects this state. const CellularPopulationBatch legacy{biology.header, biology, {}, {{0, {}}}}; auto replayed = checked(replay_population_batch(*active, legacy), "retained legacy trace"); require(replayed->snapshot() == expected->snapshot(), "old recorded prefix changed meaning"); } CellularBatch inputs_batch(const CellularPopulation& base) { CellularBatch batch{header(base, 64), {}}; auto biological = restore(base.state()); for (unsigned tick = 0; tick < 64; ++tick) { auto inputs = checked(wildlife_intents(*biological, biological->tick() + 1), "batch wildlife"); for (const auto& life : biological->lives()) { if (life.account == LifeAccount{}) continue; CellularLifeIntent frozen{life.identity.life, {}}; frozen.intent.frozen = true; inputs.push_back(frozen); } std::ranges::sort(inputs, {}, &CellularLifeIntent::life_id); batch.ticks.push_back({inputs, {}, {}}); checked(biological->advance_tick(inputs), "build biological input fixture"); } return batch; } void traces() { auto population = checked(CellularPopulation::create(interval_source(), {true, 1200, 1800}), "batch population"); std::uint64_t converted = 0; for (unsigned interval = 0; interval < 6; ++interval) { auto inputs = inputs_batch(*population); require(checked(unattended_population_inputs(population->state(), inputs.header), "unattended worker batch") == inputs, "worker interval changed frozen/wildlife input contract"); const auto predecessor = population->snapshot(); auto execution = checked(prepare_population_batch(*population, inputs), "prepare trace"); const auto bytes = checked(encode_population_batch(execution.batch), "encode trace"); const auto decoded = checked(decode_population_batch(bytes), "decode trace"); require(decoded == execution.batch, "population trace round trip"); auto replay = checked(replay_population_batch(*population, decoded), "replay trace"); require(replay->snapshot() == execution.state->snapshot() && population->snapshot() == predecessor, "trace changed predecessor or replay result"); auto altered = decoded; ++altered.boundaries.back().sequence; require(!replay_population_batch(*population, altered), "altered sequence admitted"); altered = decoded; altered.biology.ticks.front().lives.front().intent.frozen = false; require(!replay_population_batch(*population, altered), "unfrozen controlled step admitted"); for (std::size_t length = 0; length < bytes.size(); length += 113) require(!decode_population_batch(std::span{bytes}.first(length)), "truncated trace admitted"); auto extended = bytes; extended.push_back(0); require(!decode_population_batch(extended), "appended trace bytes admitted"); converted += replay->members().size(); population = std::move(replay); } require(converted > 0 && !population->members().empty(), "batch test has no cohorts"); const auto genome = population->members().front().genome; const auto before = population->state().snapshot(); auto conversion = checked(prepare_population_conversion(*population, header(*population, 0), {genome, false}), "manual trace"); const auto encoded = checked(encode_population_batch(conversion.batch), "manual encode"); const auto decoded = checked(decode_population_batch(encoded), "manual decode"); auto replay = checked(replay_population_batch(*population, decoded), "manual replay"); require(replay->state().snapshot() == before && replay->members().empty(), "manual materialization changed biology"); } void manifests() { auto state = interval_source(); auto population = checked(CellularPopulation::create(restore(*state), {true, 1200, 1800}), "manifest population"); CheckpointManifest source; source.profile = CheckpointProfile::cellular_lives_v1; source.cellular_versions = cellular_v9_versions; source.history_profile = cellular_revision_history_profile; source.key = {state->world(), state->region()}; source.frame = state->frame(); source.tick_microseconds = 50000; source.sequence = 2; source.commit = filled<16>(1); source.predecessor_commit = filled<16>(2); source.predecessor_digest = filled<32>(3); source.holder = filled<16>(4); source.epoch = 1; source.begin_tick = state->tick() - 1; source.end_tick = state->tick(); source.build = filled<32>(5); source.payload_bytes = state->snapshot().size(); source.payload_sha256 = filled<32>(6); source.batch = {128, filled<32>(7), 0, {}}; require(encode_manifest(source).has_value(), "source manifest fixture"); LeaseGrant lease{filled<16>(4), 1, state->frame(), std::chrono::milliseconds{20000}}; auto next = checked(population_profile_transition_manifest( source, filled<32>(8), filled<16>(9), lease, source.build, population->snapshot().size(), filled<32>(10)), "profile transition"); require(next.profile == CheckpointProfile::cellular_population_v1 && supported(next), "wrong profile"); const auto encoded = checked(encode_manifest(next), "population manifest"); require(checked(decode_manifest(encoded), "manifest decode") == next, "manifest profile changed"); auto restored_population = checked(restore_population_payload(population->snapshot(), next), "profile payload"); require(restored_population->snapshot() == population->snapshot(), "population payload changed"); require(!restore_cellular_payload(population->snapshot(), next), "population mislabeled as cellular"); auto future = checked(population->migration_upgrade_candidate(), "future portable envelope"); auto mislabeled = next; mislabeled.payload_bytes = future->snapshot().size(); auto refused = restore_population_payload(future->snapshot(), mislabeled); require(!refused && refused.error() == CheckpointError::unsupported, "portable envelope 2 bypassed durable bundle admission"); auto raw_mislabeled = source; raw_mislabeled.payload_bytes = future->state().snapshot().size(); require(!restore_cellular_payload(future->state().snapshot(), raw_mislabeled), "portable owner 7 bypassed raw durable bundle admission"); auto back = checked(population_profile_transition_manifest( next, filled<32>(11), filled<16>(12), lease, source.build, state->snapshot().size(), filled<32>(13)), "reverse transition"); require(back.end_tick == source.end_tick && back.profile == source.profile && checked(restore_cellular_payload(state->snapshot(), back), "unwrapped payload") ->snapshot() == state->snapshot(), "profile transition changed biology/time"); auto execution = checked(prepare_population_batch(*population, inputs_batch(*population)), "manifest tick fixture"); const auto trace = checked(encode_population_batch(execution.batch), "manifest trace fixture"); const auto advancing = population_successor_manifest( next, filled<32>(11), filled<16>(12), lease, execution.state->state().tick(), source.build, execution.state->snapshot().size(), filled<32>(13), {trace.size(), filled<32>(14), 0, {}}); require(advancing.has_value() && restore_population_payload(execution.state->snapshot(), *advancing).has_value(), "population successor refused"); require(!clone_manifest(next, filled<32>(1), {9999, 1}, filled<16>(3), 100, filled<32>(4), {}), "population clone admitted without identity remapping"); auto malformed = next; malformed.sequence = 0; require(!encode_manifest(malformed), "population genesis admitted"); malformed = next; malformed.cellular_versions = cellular_v8_versions; require(!supported(malformed), "unsupported nested rules admitted"); } } // namespace int main() try { traces(); lingering_is_not_new_population_work(); manifests(); return 0; } catch (const std::exception& error) { std::fprintf(stderr, "%s\n", error.what()); return 1; }