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
+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);