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:
+66
-23
@@ -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();
|
||||
|
||||
Reference in New Issue
Block a user