Files
aevolution/tests/population_checkpoint_tests.cpp
dingo 463b091056
Foundation / checks (deploy) (push) Successful in 6s
Foundation / checks (ecs) (push) Successful in 2m24s
Foundation / checks (network) (push) Successful in 20m40s
Foundation / checks (client) (push) Successful in 21m57s
Foundation / checks (tsan) (push) Successful in 26m32s
Foundation / checks (postgres) (push) Successful in 1h3m17s
Foundation / checks (checks) (push) Successful in 1h14m29s
Foundation / postgres-tools (push) Successful in 34s
Foundation / server-image (push) Successful in 4m6s
Foundation / image (push) Successful in 4m52s
feat(persistence): admit durable funded maturation upgrades
2026-10-19 06:23:51 -04:00

481 lines
27 KiB
C++

#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::size_t N> std::array<std::uint8_t, N> filled(std::uint8_t byte) {
std::array<std::uint8_t, N> 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");
const auto member = std::ranges::find_if(
future->state().lives(), [](const auto& life) { return life.account == LifeAccount{}; });
require(member != future->state().lives().end(), "future trace fixture lacks wildlife");
auto future_conversion = checked(
prepare_population_conversion(*future, header(*future, 0), {member->identity.genome, true}),
"future portable conversion");
require(future_conversion.batch.format_version == 2,
"future owner recorded historical population trace");
auto legacy_replay = replay_population_batch(*population, future_conversion.batch);
require(!legacy_replay && legacy_replay.error() == CellularPopulationError::unsupported,
"new trace reinterpreted for old owner");
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");
// ADR-0157: portable maturation must not acquire durable rules-8 authority.
auto maturation = checked(future->maturation_upgrade_candidate(), "portable maturation");
auto rules8_population = next;
rules8_population.cellular_versions = cellular_v10_versions;
rules8_population.payload_bytes = future->snapshot().size();
require(restore_population_payload(future->snapshot(), rules8_population).has_value() &&
!restore_population_payload(maturation->snapshot(), rules8_population),
"rules 9 bypassed durable population admission");
auto rules8_raw = source;
rules8_raw.cellular_versions = cellular_v10_versions;
rules8_raw.payload_bytes = future->state().snapshot().size();
require(restore_cellular_payload(future->state().snapshot(), rules8_raw).has_value() &&
!restore_cellular_payload(maturation->state().snapshot(), rules8_raw),
"rules 9 bypassed durable raw admission");
auto unsupported_rules = rules8_population;
unsupported_rules.cellular_versions.rules = cellular_lives_maturation_rules + 1;
require(!supported(unsupported_rules) && !encode_manifest(unsupported_rules) &&
population_envelope_version(unsupported_rules.cellular_versions) == 0,
"unknown rules silently entered a durable bundle");
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");
// Both retained representations have one explicit no-tick accounting
// upgrade. Exercise a real cohort, including its nonzero conversion sequence.
const auto wildlife = std::ranges::find_if(population->state().lives(), [](const auto& life) {
return life.account == LifeAccount{};
});
require(wildlife != population->state().lives().end(), "upgrade lacks real wildlife");
auto converted = checked(prepare_population_conversion(*population, header(*population, 0),
{wildlife->identity.genome, true}),
"upgrade source cohort");
const auto conversion_trace =
checked(encode_population_batch(converted.batch), "upgrade source conversion trace");
auto cohort_manifest =
checked(population_successor_manifest(next, filled<32>(11), filled<16>(12), lease,
next.end_tick, next.build,
converted.state->snapshot().size(), filled<32>(13),
{conversion_trace.size(), filled<32>(14), 0, {}}),
"upgrade cohort predecessor");
require(!converted.state->members().empty() && converted.state->conversion_sequence() != 0,
"upgrade source lacks membership history");
const std::array bases{source, cohort_manifest};
const std::array payloads{state->snapshot(), converted.state->snapshot()};
for (std::size_t index = 0; index < bases.size(); ++index) {
const auto& base = bases[index];
const auto& payload = payloads[index];
auto upgraded_payload =
checked(cellular_boundary_upgrade_payload(payload, base), "boundary payload upgrade");
auto upgraded = checked(cellular_boundary_upgrade_manifest(
base, filled<32>(15), filled<16>(16), lease, filled<32>(17),
upgraded_payload.size(), filled<32>(18)),
"boundary manifest upgrade");
auto expected = base;
expected.cellular_versions = cellular_v10_versions;
expected.commit = filled<16>(16);
++expected.sequence;
expected.predecessor_commit = base.commit;
expected.predecessor_digest = filled<32>(15);
expected.begin_tick = base.end_tick;
expected.holder = lease.holder;
expected.epoch = lease.epoch;
expected.build = filled<32>(17);
expected.payload_bytes = upgraded_payload.size();
expected.payload_sha256 = filled<32>(18);
expected.batch = {};
require(upgraded == expected && supported(upgraded) &&
checked(decode_manifest(checked(encode_manifest(upgraded), "upgrade encode")),
"upgrade decode") == upgraded,
"boundary upgrade changed profile, clock, history or identity");
auto matured_payload =
checked(cellular_maturation_upgrade_payload(upgraded_payload, upgraded),
"maturation payload upgrade");
auto matured = checked(cellular_maturation_upgrade_manifest(
upgraded, filled<32>(19), filled<16>(20), lease, upgraded.build,
matured_payload.size(), filled<32>(21)),
"maturation manifest upgrade");
auto expected_matured = upgraded;
expected_matured.cellular_versions = cellular_v11_versions;
expected_matured.commit = filled<16>(20);
++expected_matured.sequence;
expected_matured.predecessor_commit = upgraded.commit;
expected_matured.predecessor_digest = filled<32>(19);
expected_matured.payload_bytes = matured_payload.size();
expected_matured.payload_sha256 = filled<32>(21);
require(matured == expected_matured && supported(matured) &&
checked(decode_manifest(checked(encode_manifest(matured), "maturation encode")),
"maturation decode") == matured,
"maturation upgrade changed profile, identity, stock metadata or time");
require(!cellular_maturation_upgrade_payload(payload, base) &&
!cellular_maturation_upgrade_payload(matured_payload, matured) &&
!cellular_boundary_upgrade_payload(matured_payload, matured),
"maturation skipped prerequisite, repeated or downgraded");
if (index == 0) {
auto old = checked(restore_cellular_payload(upgraded_payload, upgraded),
"maturation raw source");
auto target = checked(restore_cellular_payload(matured_payload, matured),
"maturation raw target");
require(target->snapshot() ==
checked(old->maturation_upgrade_candidate(), "raw maturation oracle")
->snapshot() &&
target->current_total() == old->current_total(),
"raw maturation changed stocks");
} else {
auto old = checked(restore_population_payload(upgraded_payload, upgraded),
"maturation cohort source");
auto target = checked(restore_population_payload(matured_payload, matured),
"maturation cohort target");
require(target->snapshot() ==
checked(old->maturation_upgrade_candidate(), "cohort maturation oracle")
->snapshot() &&
target->conversion_sequence() == old->conversion_sequence() &&
std::ranges::equal(target->members(), old->members()),
"maturation lost cohort history");
auto biology =
checked(unattended_population_inputs(target->state(), header(*target, 64)),
"matured intentions");
auto advanced =
checked(prepare_population_batch(*target, std::move(biology)), "matured interval");
const auto matured_trace =
checked(encode_population_batch(advanced.batch), "matured trace");
auto matured_next =
checked(population_successor_manifest(
matured, filled<32>(22), filled<16>(23), lease, matured.end_tick + 64,
matured.build, advanced.state->snapshot().size(), filled<32>(24),
{matured_trace.size(), filled<32>(25), 0, {}}),
"matured successor");
require(matured_next.cellular_versions == cellular_v11_versions &&
restore_population_payload(advanced.state->snapshot(), matured_next)
.has_value(),
"matured population cannot continue");
}
auto switched = checked(population_profile_transition_manifest(
matured, filled<32>(22), filled<16>(23), lease, matured.build,
matured.payload_bytes, matured.payload_sha256),
"matured profile switch");
require(switched.cellular_versions == cellular_v11_versions &&
switched.profile != matured.profile,
"representation switch changed maturation rules");
auto genesis_matured = matured;
genesis_matured.sequence = 0;
require(!encode_manifest(genesis_matured), "maturation bypassed retained opening history");
if (index == 0) {
auto restored = checked(restore_cellular_payload(upgraded_payload, upgraded),
"upgraded raw restore");
require(
restored->snapshot() ==
checked(state->migration_upgrade_candidate(), "raw upgrade oracle")->snapshot(),
"raw boundary upgrade differs");
const auto trace_bytes = cellular_batch_header_bytes + cellular_batch_tick_header_bytes;
auto continued = checked(
successor_manifest(upgraded, filled<32>(19), filled<16>(20), lease,
upgraded.end_tick + 1, upgraded.build, upgraded.payload_bytes,
upgraded.payload_sha256, {trace_bytes, filled<32>(21), 0, {}}),
"raw upgraded successor");
require(continued.cellular_versions == cellular_v10_versions &&
continued.profile == upgraded.profile &&
continued.history_profile == upgraded.history_profile,
"ordinary raw successor changed accounting bundle");
} else {
auto restored = checked(restore_population_payload(upgraded_payload, upgraded),
"upgraded population restore");
require(restored->policy() == converted.state->policy() &&
restored->conversion_sequence() == converted.state->conversion_sequence() &&
std::ranges::equal(restored->members(), converted.state->members()),
"accounting upgrade reset cohort membership, policy or sequence");
auto biology =
checked(unattended_population_inputs(restored->state(), header(*restored, 4)),
"upgraded continuation intentions");
auto continued =
checked(prepare_population_batch(*restored, biology), "upgraded continuation");
auto recorded =
checked(encode_population_batch(continued.batch), "upgraded continuation trace");
auto continued_manifest =
checked(population_successor_manifest(
upgraded, filled<32>(19), filled<16>(20), lease, upgraded.end_tick + 4,
upgraded.build, continued.state->snapshot().size(), filled<32>(21),
{recorded.size(), filled<32>(22), 0, {}}),
"upgraded population successor");
require(continued.batch.format_version == 2 &&
restore_population_payload(continued.state->snapshot(), continued_manifest)
.has_value(),
"upgraded population cannot continue under bundle 10");
}
auto transition =
checked(population_profile_transition_manifest(
upgraded, filled<32>(19), filled<16>(20), lease, upgraded.build,
upgraded.payload_bytes, upgraded.payload_sha256),
"upgraded representation transition");
require(transition.cellular_versions == upgraded.cellular_versions &&
transition.profile != upgraded.profile &&
transition.end_tick == upgraded.end_tick,
"representation transition changed accounting bundle or time");
require(!cellular_boundary_upgrade_payload(upgraded_payload, upgraded) &&
!cellular_boundary_upgrade_manifest(
upgraded, filled<32>(19), filled<16>(20), lease, upgraded.build,
upgraded.payload_bytes, upgraded.payload_sha256),
"accounting upgrade applied twice");
auto wrong_bundle = upgraded;
wrong_bundle.cellular_versions = cellular_v9_versions;
require(index == 0 ? !restore_cellular_payload(upgraded_payload, wrong_bundle)
: !restore_population_payload(upgraded_payload, wrong_bundle),
"new payload admitted under old manifest");
wrong_bundle = base;
wrong_bundle.cellular_versions = cellular_v10_versions;
require(index == 0 ? !restore_cellular_payload(payload, wrong_bundle)
: !restore_population_payload(payload, wrong_bundle),
"old payload admitted under new manifest");
auto corrupt = payload;
corrupt.back() ^= 1;
require(!cellular_boundary_upgrade_payload(corrupt, base) &&
!cellular_boundary_upgrade_payload(std::span{payload}.first(16), base),
"corrupt or truncated upgrade source admitted");
for (unsigned fault = 0; fault < 8; ++fault) {
auto bad_base = base;
auto bad_lease = lease;
auto ref = filled<32>(15);
auto id = filled<16>(16);
switch (fault) {
case 0:
bad_base.sequence = ~std::uint64_t{0};
break;
case 1:
bad_base.sequence = 0;
break;
case 2:
bad_base.cellular_versions = cellular_v8_versions;
break;
case 3:
++bad_lease.frame;
break;
case 4:
bad_lease.epoch = 0;
break;
case 5:
bad_lease.holder = {};
break;
case 6:
ref = {};
break;
case 7:
id = base.commit;
break;
default:
require(false, "unknown upgrade fault fixture");
break;
}
require(!cellular_boundary_upgrade_manifest(bad_base, ref, id, bad_lease,
upgraded.build, upgraded.payload_bytes,
upgraded.payload_sha256),
"invalid accounting upgrade binding admitted");
}
auto genesis = upgraded;
genesis.sequence = 0;
genesis.predecessor_commit = {};
genesis.predecessor_digest = {};
genesis.holder = {};
genesis.epoch = 0;
genesis.begin_tick = genesis.end_tick = 0;
require(!encode_manifest(genesis), "boundary bundle provisioned without retained upgrade");
}
require(population_envelope_version(cellular_v8_versions) == 0 &&
population_envelope_version(cellular_v9_versions) == 1 &&
population_envelope_version(cellular_v10_versions) == 2 &&
population_envelope_version(cellular_v11_versions) == 2,
"population bundle to envelope mapping differs");
}
} // 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;
}