Files
aevolution/tests/mutation_tests.cpp
T
dingoandClaude Opus 5 badd28fe9b Check the context an offer records, not the list it produced
Selection established that a context was unchanged by regenerating the
candidate list and comparing it. That cannot establish it. Four of nine
operators is only 126 distinct lists, so different contexts draw the same
one routinely, and an offer from event 1 was accepted under event 239.

An offer now records what produced it: the parent's identity and digest,
and the context itself. Selection compares those directly, before
anything else. Because the context and parent then determine the draw, a
list that still differs means the offer itself was altered, which is now
reported as its own error rather than as a context having moved.

The regression searches for a context whose draw collides with the
offer's and asserts that such a collision exists rather than assuming
one, then requires selection to reject it. It was confirmed to fail
against the previous implementation.

One expectation of mine was wrong and is now stated correctly: rewriting
an offer's recorded context to name the colliding one 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
that the recorded context matches the current one, not provenance.
Editing a candidate remains caught, as an altered offer.

Native Release, clang-tidy analysis, ASan and format checks pass 20/20.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-23 15:37:13 -04:00

338 lines
16 KiB
C++

#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[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 = 4;
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
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();
std::cout << "PASS mutation catalog, uniform seeded offers and guarded selection\n";
return 0;
} catch (...) {
return 1;
}