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