Files
aevolution/tests/mutation_tests.cpp
T
dingoandClaude Opus 5.5 86ee800f32
Foundation / checks (ecs) (push) Successful in 3m21s
Foundation / checks (deploy) (push) Successful in 5m39s
Foundation / checks (tsan) (push) Successful in 11m43s
Foundation / checks (network) (push) Successful in 22m3s
Foundation / checks (client) (push) Successful in 22m35s
Foundation / checks (checks) (push) Successful in 46m53s
Foundation / checks (postgres) (push) Successful in 48m22s
Foundation / postgres-tools (push) Successful in 1m17s
Foundation / image (push) Successful in 3m31s
Foundation / server-image (push) Successful in 6m7s
Cellular rules 7: percentage mutations, skilled budding, hunting wildlife (ADR-0137)
The region 6 playtest found a speed mutation adding 6,667 um/s (1/300 of the range,
33x a founder), so buds seemed to teleport; reproduction made buds bigger and dearer;
and wildlife never hunted. Rules 7:

- genome schema 4: a step is a tenth of the current value (at least 1); an acquired
  capability starts at the founder's value instead of a minimum it could never grow from;
- development 4: a bud is always half the body, and reproduction takes value/2000 off
  its construction energy (founders save a quarter; matter is never discounted);
- twins: a seeded roll (reproduction/2 per thousand) splits one affordable bud's
  investment into two quarter-size buds;
- wildlife engulfs a touching cell the region step lets it take, players' included.

Founder 3, owner format 6 (no new state), bundle v8 and schema v42; the cellular-v6
opening provisions it. The client's mutation callout shrinks to a "Mutation [E]" pill and
its candidate texts describe the new steps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-16 22:41:55 -04:00

461 lines
22 KiB
C++

#include "evo/biology/founder.hpp"
#include "evo/biology/mutation.hpp"
#include <algorithm>
#include <array>
#include <iostream>
#include <map>
#include <source_location>
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
namespace {
using namespace evo::biology;
using namespace evo::accounting;
void check(bool condition, std::source_location location = std::source_location::current()) {
if (!condition) {
throw std::runtime_error("mutation line " + std::to_string(location.line()));
}
}
#define CHECK(expression) check(static_cast<bool>(expression))
constexpr Inventory pooled(std::uint64_t value) {
return {{value}, {Femtograms{value}, Femtograms{value}, Femtograms{value}, Femtograms{value}}};
}
constexpr EnvironmentState temperate{293'150, pooled(1'000'000'000)};
GenomeDraft draft() {
return {7,
1,
1,
{},
{{Capability::energy_conversion, 400, 0},
{Capability::membrane, 120, 0},
{Capability::target_size, 8'000'000, 0}}};
}
Genome build(const GenomeDraft& source) {
const auto genome = Genome::create(source);
check(genome.has_value());
return *genome;
}
MutationContext context(std::uint64_t seed, std::uint16_t count = 4) {
return {seed, 1, 1, count, {1000, 0}, temperate};
}
void the_same_context_always_offers_the_same_candidates() {
const auto genome = build(draft());
const auto first = mutation_candidates(genome, context(99));
const auto again = mutation_candidates(genome, context(99));
CHECK(first && again && *first == *again); // A retry is not a reroll.
CHECK(first->candidates.size() == 4);
// A new event earns a new draw; the seed and the stream do too.
auto later = context(99);
later.event_index = 2;
const auto next_event = mutation_candidates(genome, later);
CHECK(next_event && next_event->candidates != first->candidates);
auto elsewhere = context(99);
elsewhere.stream = 2;
CHECK(mutation_candidates(genome, elsewhere));
CHECK(mutation_candidates(genome, elsewhere)->candidates != first->candidates);
CHECK(mutation_candidates(genome, context(100))->candidates != first->candidates);
// Context that changes the organism changes the draw as well.
auto grown = context(99);
grown.growth = {500, 0};
CHECK(mutation_candidates(genome, grown)->candidates != first->candidates);
}
void offers_are_canonical_and_unranked() {
const auto genome = build(draft());
bool ever_unsorted_by_cost = false;
for (std::uint64_t seed = 0; seed < 64; ++seed) {
const auto offered = mutation_candidates(genome, context(seed));
CHECK(offered && offered->candidates.size() == 4);
// Canonical order, so position cannot encode a preference. Strictly
// increasing, because a draw without replacement never repeats one.
for (std::size_t index = 1; index < offered->candidates.size(); ++index) {
CHECK(offered->candidates[index - 1].operator_id <
offered->candidates[index].operator_id);
}
if (!std::ranges::is_sorted(offered->candidates, {}, &MutationCandidate::maintenance)) {
ever_unsorted_by_cost = true;
}
}
// Offers are not arranged by their reported cost either, so no position in
// the list is the cheap one or the good one.
CHECK(ever_unsorted_by_cost);
}
// The operators a founder genome with conversion, membrane and target size can
// legitimately reach. Derived by hand from the schema: required capabilities
// cannot be shed, absent ones cannot be adjusted, present ones cannot be
// acquired, and target size cannot step down from 8e6 by its 9,999,999 step.
std::vector<std::pair<Capability, MutationEffect>> expected_eligible() {
return {{Capability::energy_conversion, MutationEffect::adjust_up},
{Capability::energy_conversion, MutationEffect::adjust_down},
{Capability::membrane, MutationEffect::adjust_up},
{Capability::membrane, MutationEffect::adjust_down},
{Capability::movement, MutationEffect::acquire},
{Capability::ingestion, MutationEffect::acquire},
{Capability::reproduction, MutationEffect::acquire},
{Capability::target_size, MutationEffect::adjust_up},
{Capability::thermal_tolerance, MutationEffect::acquire}};
}
void every_eligible_operator_is_offered_and_none_is_favoured() {
const auto genome = build(draft());
constexpr std::uint64_t trials = 2000;
constexpr std::uint64_t drawn = 4;
std::map<std::uint16_t, std::uint64_t> offers;
for (std::uint64_t seed = 0; seed < trials; ++seed) {
const auto offered = mutation_candidates(genome, context(seed));
CHECK(offered && offered->candidates.size() == drawn);
for (const auto& candidate : offered->candidates) {
++offers[candidate.operator_id];
}
}
// The set that was offered is exactly the set the schema permits, no more.
const auto permitted = expected_eligible();
CHECK(offers.size() == permitted.size());
for (const auto& [kind, effect] : permitted) {
const auto entry = std::ranges::find_if(mutation_catalog(), [&](const auto& candidate) {
return candidate.target == kind && candidate.effect == effect;
});
CHECK(entry != mutation_catalog().end() && offers.contains(entry->id));
}
// Each is offered within a fifth of its exact share. The draw is uniform by
// construction, so this is about six standard deviations wide; a draw that
// favoured any operator would be the biased-adaptation failure.
const auto share = trials * drawn / permitted.size();
for (const auto& [id, count] : offers) {
CHECK(count * 5 > share * 4);
CHECK(count * 4 < share * 5);
}
}
void only_legitimate_operators_are_offered() {
const auto genome = build(draft());
const auto offered = mutation_candidates(genome, context(5, 8));
CHECK(offered);
for (const auto& candidate : offered->candidates) {
// A required capability can never be shed, however it is drawn.
const auto& limits = (*capability_schema_for(
genome.schema_version()))[static_cast<std::size_t>(candidate.target)];
CHECK(!(candidate.effect == MutationEffect::shed && limits.required));
// Acquiring only ever applies to something the parent lacks, and
// adjusting only to something it carries.
if (candidate.effect == MutationEffect::acquire) {
CHECK(!genome.carries(candidate.target));
CHECK(candidate.parameter == limits.minimum);
} else if (candidate.effect != MutationEffect::shed) {
CHECK(genome.carries(candidate.target));
CHECK(candidate.parameter >= limits.minimum && candidate.parameter <= limits.maximum);
}
// Every offer produces a genome the schema accepts.
const auto successor =
select_mutation(genome, context(5, 8), *offered, candidate.operator_id, 99, 42);
CHECK(successor);
CHECK(successor->id() == 99 && successor->origin_event() == 42);
CHECK(successor->parents().size() == 1 && successor->parents()[0] == 7);
CHECK(Genome::decode(successor->encode()));
}
}
void boundaries_are_refused_rather_than_clamped() {
// At the top of its range, adjusting a capability up is not on offer at all.
auto source = draft();
source.capabilities[1].parameter = 10'000; // membrane at its maximum
const auto topped = build(source);
const auto offers = mutation_candidates(topped, context(3, 8));
CHECK(offers);
for (const auto& candidate : offers->candidates) {
CHECK(!(candidate.target == Capability::membrane &&
candidate.effect == MutationEffect::adjust_up));
}
// The same at the bottom.
source.capabilities[1].parameter = 1;
const auto floored = build(source);
const auto low = mutation_candidates(floored, context(3, 8));
CHECK(low);
for (const auto& candidate : low->candidates) {
CHECK(!(candidate.target == Capability::membrane &&
candidate.effect == MutationEffect::adjust_down));
}
// A capability another one declares as a prerequisite cannot be shed, and no
// rule here says so: the schema's own validation refuses it.
source = draft();
source.capabilities[1].parameter = 120;
source.capabilities.push_back({Capability::thermal_tolerance, 500, 0});
source.capabilities.push_back(
{Capability::movement, 250, capability_bit(Capability::thermal_tolerance)});
const auto dependent = build(source);
const auto guarded = mutation_candidates(dependent, context(11, 8));
CHECK(guarded);
for (const auto& candidate : guarded->candidates) {
CHECK(!(candidate.target == Capability::thermal_tolerance &&
candidate.effect == MutationEffect::shed));
}
// Shedding the dependent capability itself is still legitimate.
const auto sheddable = std::ranges::find_if(guarded->candidates, [](const auto& candidate) {
return candidate.target == Capability::movement && candidate.effect == MutationEffect::shed;
});
if (sheddable != guarded->candidates.end()) {
const auto successor =
select_mutation(dependent, context(11, 8), *guarded, sheddable->operator_id, 99, 42);
CHECK(successor && !successor->carries(Capability::movement));
}
}
void selection_refuses_a_changed_context_or_an_unknown_choice() {
const auto genome = build(draft());
const auto offered = mutation_candidates(genome, context(7));
CHECK(offered);
const auto chosen = offered->candidates.front().operator_id;
CHECK(select_mutation(genome, context(7), *offered, chosen, 99, 42));
// An operator that was never offered is refused even though it is valid.
std::uint16_t absent = 0;
for (const auto& entry : mutation_catalog()) {
if (std::ranges::find(offered->candidates, entry.id, &MutationCandidate::operator_id) ==
offered->candidates.end()) {
absent = entry.id;
break;
}
}
CHECK(absent != 0);
const auto unknown = select_mutation(genome, context(7), *offered, absent, 99, 42);
CHECK(!unknown && unknown.error() == MutationError::unknown_candidate);
// A context that moved is rejected outright, never answered with a
// different mutation that happens to still be valid.
auto moved = context(7);
moved.event_index = 5;
const auto stale = select_mutation(genome, moved, *offered, chosen, 99, 42);
CHECK(!stale && stale.error() == MutationError::context_changed);
auto regrown = context(7);
regrown.growth = {250, 0};
const auto shifted = select_mutation(genome, regrown, *offered, chosen, 99, 42);
CHECK(!shifted && shifted.error() == MutationError::context_changed);
// A tampered offer is a changed context too.
auto edited = *offered;
edited.candidates.front().maintenance += 1;
const auto forged = select_mutation(genome, context(7), edited, chosen, 99, 42);
// An altered list is not a context that moved, and is named separately.
CHECK(!forged && forged.error() == MutationError::altered_offer);
// A different parent genome cannot be answered with this offer.
auto other = draft();
other.capabilities[0].parameter = 401;
const auto cousin = build(other);
const auto mismatched = select_mutation(cousin, context(7), *offered, chosen, 99, 42);
CHECK(!mismatched && mismatched.error() == MutationError::context_changed);
}
void identical_offers_from_different_contexts_are_still_rejected() {
const auto genome = build(draft());
const auto base = context(7);
const auto offer = mutation_candidates(genome, base);
CHECK(offer);
// Four of nine operators is only 126 distinct lists, so different contexts
// draw the same one often. Comparing lists would accept a stale offer.
std::uint64_t collided = 0;
for (std::uint64_t event = 2; event < 1000 && collided == 0; ++event) {
auto other = base;
other.event_index = event;
const auto drawn = mutation_candidates(genome, other);
CHECK(drawn);
if (drawn->candidates == offer->candidates) {
collided = event;
}
}
// The collision this guards against must actually exist, or the test below
// would pass without exercising anything.
CHECK(collided != 0);
auto stale = base;
stale.event_index = collided;
const auto chosen = offer->candidates.front().operator_id;
const auto refused = select_mutation(genome, stale, *offer, chosen, 99, 42);
CHECK(!refused && refused.error() == MutationError::context_changed);
// The offer is still good under the context it was actually drawn from.
CHECK(select_mutation(genome, base, *offer, chosen, 99, 42));
// Rewriting the record to claim the collided context is accepted, and should
// be: those candidates genuinely are what that context draws, so the result
// is a legitimate offer for it rather than a forgery. The check is on the
// recorded context matching the current one, not on provenance.
auto rewritten = *offer;
rewritten.context.event_index = collided;
CHECK(select_mutation(genome, stale, rewritten, chosen, 99, 42));
// Editing a candidate is the forgery the list comparison still catches.
rewritten.candidates.front().total_mass += 1;
const auto forged = select_mutation(genome, stale, rewritten, chosen, 99, 42);
CHECK(!forged && forged.error() == MutationError::altered_offer);
}
void reported_cost_is_what_the_budget_charges() {
const auto genome = build(draft());
const auto offered = mutation_candidates(genome, context(13, 8));
CHECK(offered);
for (const auto& candidate : offered->candidates) {
const auto successor =
select_mutation(genome, context(13, 8), *offered, candidate.operator_id, 99, 42);
CHECK(successor);
// The candidate showed exactly what ADR-0032 would charge; nothing here
// invents a price, and nothing was reserved by displaying it.
const auto budget = metabolic_budget(*successor, {1000, 0}, temperate);
CHECK(budget && budget->maintenance.value == candidate.maintenance);
CHECK(budget->phenotype.total_mass == candidate.total_mass);
}
// The parent is untouched by every one of those proposals.
CHECK(genome == build(draft()));
}
void invalid_contexts_are_refused() {
const auto genome = build(draft());
for (const std::uint16_t count :
{std::uint16_t{0}, static_cast<std::uint16_t>(max_candidates + 1)}) {
const auto refused = mutation_candidates(genome, context(1, count));
CHECK(!refused && refused.error() == MutationError::invalid_context);
}
// An unsupported development version leaves no eligible operator at all,
// because every successor fails to derive a budget.
auto source = draft();
source.development_version = 5;
const auto future = build(source);
const auto empty = mutation_candidates(future, context(1));
CHECK(!empty && empty.error() == MutationError::no_candidates);
CHECK(mutation_catalog().size() == 4 * capability_count);
}
} // namespace
// ADR-0099: a successor keeps its parent's schema and that schema's adjustment step, so
// a schema 1 lineage is never migrated by mutating, and a schema 2 one steps per second.
// Schema 3 (rules 6) steps a third as far: 1/300 of the range.
void successors_keep_their_parents_schema() {
for (const auto schema :
{genome_schema_v1, genome_schema_v2, genome_schema_v3, genome_schema_v4}) {
auto source = draft();
source.schema_version = schema;
source.capabilities.push_back(
{Capability::movement, schema == genome_schema_v1 ? 50'000U : 1'000'000U, 0});
const auto parent = build(source);
const auto step = schema == genome_schema_v1 ? 1'000U // 1/100 of the range.
: schema == genome_schema_v2 ? 20'000U
: schema == genome_schema_v3 ? 6'666U // 2,000,000 / 300.
: 100'000U; // A tenth of 1,000,000.
bool adjusted = false;
// Offers are seeded; search seeds (deterministically) for the movement adjustment.
for (std::uint64_t seed = 1; seed <= 200 && !adjusted; ++seed) {
const auto offered = mutation_candidates(parent, context(seed, 8));
CHECK(offered);
for (const auto& candidate : offered->candidates) {
const auto successor = select_mutation(parent, context(seed, 8), *offered,
candidate.operator_id, 99, 42);
CHECK(successor && successor->schema_version() == schema);
if (candidate.target == Capability::movement &&
candidate.effect == MutationEffect::adjust_up) {
CHECK(*successor->parameter(Capability::movement) ==
*parent.parameter(Capability::movement) + step);
adjusted = true;
}
}
}
CHECK(adjusted);
}
}
// ADR-0137 (schema 4): a step is a tenth of the current value, down as well as up, and an
// acquired capability starts at the founder's value instead of the schema minimum.
void schema_four_steps_by_percentage() {
const auto find = [](const Genome& parent, Capability target, MutationEffect effect) {
for (std::uint64_t seed = 1; seed <= 400; ++seed) {
const auto offered = mutation_candidates(parent, context(seed, 8));
CHECK(offered);
for (const auto& candidate : offered->candidates)
if (candidate.target == target && candidate.effect == effect) {
const auto successor = select_mutation(parent, context(seed, 8), *offered,
candidate.operator_id, 99, 42);
CHECK(successor.has_value());
return successor.value_or(parent);
}
}
throw std::runtime_error("no such candidate");
};
auto source = draft();
source.schema_version = genome_schema_v4;
source.capabilities.push_back({Capability::movement, 200, 0});
const auto swimmer = build(source);
CHECK(*find(swimmer, Capability::movement, MutationEffect::adjust_up)
.parameter(Capability::movement) == 220);
CHECK(*find(swimmer, Capability::movement, MutationEffect::adjust_down)
.parameter(Capability::movement) == 180);
source.capabilities.back().parameter = 5; // Below ten, a step is still 1.
CHECK(*find(build(source), Capability::movement, MutationEffect::adjust_up)
.parameter(Capability::movement) == 6);
source.capabilities.pop_back();
const auto still = build(source);
CHECK(*find(still, Capability::movement, MutationEffect::acquire)
.parameter(Capability::movement) == primitive_founder_parameters.movement);
}
void mutation_funding_is_exact_and_atomic() {
const auto parent = build(draft());
const auto context_value = context(33, 8);
const auto offered = mutation_candidates(parent, context_value);
CHECK(offered);
constexpr Inventory reserve = pooled(1'000'000'000);
for (const auto& candidate : offered->candidates) {
const auto transition =
fund_mutation(parent, context_value, *offered, candidate.operator_id, 99, 42, reserve);
CHECK(transition);
const auto before =
metabolic_budget(parent, context_value.growth, context_value.environment);
CHECK(before);
for (std::size_t channel = 0; channel < reserve.matter.size(); ++channel) {
CHECK(before->body.matter[channel].value + reserve.matter[channel].value ==
transition->body.matter[channel].value +
transition->reserve.matter[channel].value);
}
CHECK(reserve.energy.value - transition->reserve.energy.value ==
transition->intake.energy.value);
CHECK(transition->successor.parents().size() == 1 &&
transition->successor.parents().front() == parent.id());
}
// Acquiring a larger target-size capability needs construction matter and
// energy. Empty reserves refuse the proposal and leave the caller's balance
// unchanged.
bool checked_unfunded = false;
for (std::uint64_t seed = 1; seed <= 200 && !checked_unfunded; ++seed) {
const auto offer = mutation_candidates(parent, context(seed, 8));
CHECK(offer);
for (const auto& candidate : offer->candidates) {
if (candidate.target != Capability::target_size ||
candidate.effect != MutationEffect::adjust_up) {
continue;
}
constexpr Inventory empty{};
const auto result = fund_mutation(parent, context(seed, 8), *offer,
candidate.operator_id, 99, 42, empty);
CHECK(!result && result.error() == MutationError::insufficient_funding);
CHECK(empty == Inventory{});
checked_unfunded = true;
break;
}
}
CHECK(checked_unfunded);
}
int main() try {
the_same_context_always_offers_the_same_candidates();
offers_are_canonical_and_unranked();
only_legitimate_operators_are_offered();
boundaries_are_refused_rather_than_clamped();
selection_refuses_a_changed_context_or_an_unknown_choice();
identical_offers_from_different_contexts_are_still_rejected();
reported_cost_is_what_the_budget_charges();
invalid_contexts_are_refused();
every_eligible_operator_is_offered_and_none_is_favoured();
successors_keep_their_parents_schema();
schema_four_steps_by_percentage();
mutation_funding_is_exact_and_atomic();
std::cout << "PASS mutation catalog, uniform seeded offers and guarded selection\n";
return 0;
} catch (...) {
return 1;
}