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>
This commit is contained in:
dingo
2026-09-23 15:37:13 -04:00
co-authored by Claude Opus 5
parent 5497f4d74c
commit badd28fe9b
5 changed files with 134 additions and 46 deletions
+11 -5
View File
@@ -55,11 +55,17 @@ reserves nothing. Cost is not invented here: it is the successor's own maintenan
and mass under ADR-0032, derived from the same rules as the parent's, so a
candidate carries a consequence rather than a price this ADR made up.
`select_mutation` takes the candidates that were offered, regenerates them from
the context, and refuses unless the whole list still matches. A changed context is
rejected outright rather than silently answered with a different mutation. Only
then is the immutable successor built, with a caller-allocated identity, the
parent recorded, and M3.1 validating it once more.
An offer therefore records what produced it: the parent's identity and digest, and
the context itself. `select_mutation` compares those against the current ones
first, and only then regenerates the list. A candidate list cannot stand in for
that comparison, because a small catalog yields few distinct lists and two
different contexts draw the same one often; comparing lists alone would accept a
stale offer. A changed context is rejected outright rather than silently answered
with a different mutation. Because the context and parent then determine the draw,
a list that still differs means the offer itself was altered, which is reported as
its own error rather than as a context having moved. Only then is the immutable
successor built, with a caller-allocated identity, the parent recorded, and M3.1
validating it once more.
## Alternatives Considered
+17
View File
@@ -620,6 +620,23 @@ and its successor checked to validate, to record parent 7, and to survive a
decode; the reported maintenance and mass are checked to equal what
`metabolic_budget` charges that successor.
Review of `5497f4d` found one defect (2026-09-23): selection established a
stale context by comparing regenerated candidate lists, which cannot establish it.
Four of nine operators is only 126 distinct lists, so different contexts draw the
same list routinely, and an offer from event 1 was accepted under event 239. An
offer now records the parent's identity and digest and the context itself, and
selection compares those directly before anything else. Because they then
determine the draw, a list that still differs means the offer was altered, which
is now its own error rather than a context having moved.
The regression searches for a context whose draw collides with the offer's,
asserts that such a collision exists rather than assuming it, and requires that
selection still rejects it. It was confirmed to fail against the previous
implementation. Rewriting the offer's recorded context to name the colliding one
is checked to be 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.
Editing a candidate is checked to be caught as an altered offer.
No organism mutates and no fixture calls any of this. Candidate count, context
window, mutation cost as a game rule, inherited versus acquired expression, NPC
mutation and recombination remain open and are not settled here. Golden
+18 -6
View File
@@ -34,11 +34,23 @@ struct MutationCandidate {
std::uint64_t total_mass;
auto operator<=>(const MutationCandidate&) const = default;
};
// What was offered, and what it was drawn from. The record carries its own
// context and parent because a candidate list cannot stand in for them: a small
// catalog yields few distinct lists, so different contexts draw the same one
// often, and comparing lists would accept a stale offer.
struct MutationOffer {
std::uint64_t parent;
std::uint64_t parent_digest;
MutationContext context;
std::vector<MutationCandidate> candidates;
auto operator<=>(const MutationOffer&) const = default;
};
enum class MutationError : std::uint8_t {
invalid_context,
no_candidates,
unknown_candidate,
context_changed,
altered_offer,
invalid_successor
};
inline constexpr std::size_t max_candidates = 8;
@@ -46,12 +58,12 @@ inline constexpr std::size_t max_candidates = 8;
[[nodiscard]] std::span<const MutationOperator> mutation_catalog() noexcept;
// Identical inputs always yield identical candidates, sorted by operator ID.
// The order is canonical and means nothing: nothing here is ranked.
[[nodiscard]] std::expected<std::vector<MutationCandidate>, MutationError>
[[nodiscard]] std::expected<MutationOffer, MutationError>
mutation_candidates(const Genome& genome, const MutationContext& context);
// Regenerates the offer and refuses unless it still matches exactly, so a changed
// context is rejected rather than answered with a different mutation.
// Compares the offer's recorded context and parent against the current ones
// before anything else, so a changed context is rejected rather than answered
// with a different mutation.
[[nodiscard]] std::expected<Genome, MutationError>
select_mutation(const Genome& genome, const MutationContext& context,
std::span<const MutationCandidate> offered, std::uint16_t chosen,
std::uint64_t successor_id, std::uint64_t origin_event);
select_mutation(const Genome& genome, const MutationContext& context, const MutationOffer& offered,
std::uint16_t chosen, std::uint64_t successor_id, std::uint64_t origin_event);
} // namespace evo::biology
+22 -12
View File
@@ -127,8 +127,8 @@ std::span<const MutationOperator> mutation_catalog() noexcept {
return catalog;
}
std::expected<std::vector<MutationCandidate>, MutationError>
mutation_candidates(const Genome& genome, const MutationContext& context) {
std::expected<MutationOffer, MutationError> mutation_candidates(const Genome& genome,
const MutationContext& context) {
if (context.candidate_count == 0 || context.candidate_count > max_candidates) {
return std::unexpected{MutationError::invalid_context};
}
@@ -167,24 +167,34 @@ mutation_candidates(const Genome& genome, const MutationContext& context) {
eligible.resize(wanted);
// Canonical order. Position carries no meaning; nothing here is ranked.
std::ranges::sort(eligible, {}, &MutationCandidate::operator_id);
return eligible;
// The offer records what produced it, so selection can check that directly.
return MutationOffer{genome.id(), genome.content_digest(), context, std::move(eligible)};
}
std::expected<Genome, MutationError>
select_mutation(const Genome& genome, const MutationContext& context,
std::span<const MutationCandidate> offered, std::uint16_t chosen,
std::uint64_t successor_id, std::uint64_t origin_event) {
select_mutation(const Genome& genome, const MutationContext& context, const MutationOffer& offered,
std::uint16_t chosen, std::uint64_t successor_id, std::uint64_t origin_event) {
// Checked first and directly. A regenerated candidate list cannot establish
// this: the catalog yields few distinct lists, so two different contexts draw
// the same one often enough that a list comparison would accept a stale offer.
// Answering a moved context with a still-valid but different mutation would
// mutate an organism into something nobody was shown.
if (offered.parent != genome.id() || offered.parent_digest != genome.content_digest() ||
offered.context != context) {
return std::unexpected{MutationError::context_changed};
}
const auto current = mutation_candidates(genome, context);
if (!current) {
return std::unexpected{current.error()};
}
// A changed context is refused outright. Answering it with a still-valid but
// different mutation would mutate an organism into something nobody saw.
if (!std::ranges::equal(*current, offered)) {
return std::unexpected{MutationError::context_changed};
// The context and parent match, so the draw is determined. A difference here
// means the offer itself was altered, which is not a context having moved.
if (current->candidates != offered.candidates) {
return std::unexpected{MutationError::altered_offer};
}
const auto picked = std::ranges::find(*current, chosen, &MutationCandidate::operator_id);
if (picked == current->end()) {
const auto picked =
std::ranges::find(current->candidates, chosen, &MutationCandidate::operator_id);
if (picked == current->candidates.end()) {
return std::unexpected{MutationError::unknown_candidate};
}
const auto entry = std::ranges::find(catalog, chosen, &MutationOperator::id);
+66 -23
View File
@@ -46,21 +46,21 @@ void the_same_context_always_offers_the_same_candidates() {
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->size() == 4);
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 != *first);
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) != *first);
CHECK(*mutation_candidates(genome, context(100)) != *first);
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) != *first);
CHECK(mutation_candidates(genome, grown)->candidates != first->candidates);
}
void offers_are_canonical_and_unranked() {
@@ -68,13 +68,14 @@ void offers_are_canonical_and_unranked() {
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->size() == 4);
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->size(); ++index) {
CHECK((*offered)[index - 1].operator_id < (*offered)[index].operator_id);
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, {}, &MutationCandidate::maintenance)) {
if (!std::ranges::is_sorted(offered->candidates, {}, &MutationCandidate::maintenance)) {
ever_unsorted_by_cost = true;
}
}
@@ -106,8 +107,8 @@ void every_eligible_operator_is_offered_and_none_is_favoured() {
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->size() == drawn);
for (const auto& candidate : *offered) {
CHECK(offered && offered->candidates.size() == drawn);
for (const auto& candidate : offered->candidates) {
++offers[candidate.operator_id];
}
}
@@ -134,7 +135,7 @@ 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) {
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));
@@ -164,7 +165,7 @@ void boundaries_are_refused_rather_than_clamped() {
const auto topped = build(source);
const auto offers = mutation_candidates(topped, context(3, 8));
CHECK(offers);
for (const auto& candidate : *offers) {
for (const auto& candidate : offers->candidates) {
CHECK(!(candidate.target == Capability::membrane &&
candidate.effect == MutationEffect::adjust_up));
}
@@ -173,7 +174,7 @@ void boundaries_are_refused_rather_than_clamped() {
const auto floored = build(source);
const auto low = mutation_candidates(floored, context(3, 8));
CHECK(low);
for (const auto& candidate : *low) {
for (const auto& candidate : low->candidates) {
CHECK(!(candidate.target == Capability::membrane &&
candidate.effect == MutationEffect::adjust_down));
}
@@ -187,15 +188,15 @@ void boundaries_are_refused_rather_than_clamped() {
const auto dependent = build(source);
const auto guarded = mutation_candidates(dependent, context(11, 8));
CHECK(guarded);
for (const auto& candidate : *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, [](const auto& candidate) {
const auto sheddable = std::ranges::find_if(guarded->candidates, [](const auto& candidate) {
return candidate.target == Capability::movement && candidate.effect == MutationEffect::shed;
});
if (sheddable != guarded->end()) {
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));
@@ -206,13 +207,13 @@ 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->front().operator_id;
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, entry.id, &MutationCandidate::operator_id) ==
offered->end()) {
if (std::ranges::find(offered->candidates, entry.id, &MutationCandidate::operator_id) ==
offered->candidates.end()) {
absent = entry.id;
break;
}
@@ -232,9 +233,10 @@ void selection_refuses_a_changed_context_or_an_unknown_choice() {
CHECK(!shifted && shifted.error() == MutationError::context_changed);
// A tampered offer is a changed context too.
auto edited = *offered;
edited.front().maintenance += 1;
edited.candidates.front().maintenance += 1;
const auto forged = select_mutation(genome, context(7), edited, chosen, 99, 42);
CHECK(!forged && forged.error() == MutationError::context_changed);
// 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;
@@ -243,11 +245,51 @@ void selection_refuses_a_changed_context_or_an_unknown_choice() {
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) {
for (const auto& candidate : offered->candidates) {
const auto successor =
select_mutation(genome, context(13, 8), *offered, candidate.operator_id, 99, 42);
CHECK(successor);
@@ -284,6 +326,7 @@ int main() try {
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();