Files
aevolution/tests/cellular_catalog_tests.cpp
dingoandClaude Opus 5.5 732d008aa1
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 4m30s
Foundation / postgres-tools (push) Successful in 22s
Foundation / server-image (push) Successful in 4m10s
Foundation / image (push) Successful in 4m46s
feat(biology): rules 13 mutation catalog, behaviour triggers and offer draw
Phase 3 of the cellular revamp build plan (ADR-0179), first part: the headless
pieces the owner will use.

- The named catalog: all 49 mutations of Cellular_mutation_plan.md as data
  (stable ids and tuning keys, name, pro, con, effect, availability, increase
  and decrease triggers, base weight). Applying one edits a schema-7 plan by
  fixed reference points (a point is a hundredth of the founder's value), with
  paired edits, spreads across a channel group, recipe shifts, photosynthesis
  acquire and shed, and Adhesion; bounds, gates (carbon burned, buds made) and
  the enabled switch close it. 43 mutations are open at birth, as planned.
- Triggers: per-tick activity signals and eight trigger kinds (accumulate,
  absence, count in a window, event, share over a window, regular recurrence,
  straight heading, sharp turns) move each mutation's affinity score within
  bounds; integer state only, so replay reproduces scores.
- Offers: weight = base x a score multiplier; chances clamp(k x weight, floor,
  cap) summing to the options, in parts per million (floor 0.001%, cap 70%),
  found exactly by water-filling over sorted weights; a systematic draw picks
  distinct mutations whose inclusion frequencies equal their chances exactly.

Tests cover the catalog's identities, the pool at birth, every kind of edit,
bounds and gates; every trigger kind; score bounds; weights; a 300-seed chance
ensemble; and an exhaustive draw over all million draws. Refreshes the DEV
session window.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 18:46:21 -04:00

208 lines
8.9 KiB
C++

#include "evo/biology/cellular_catalog.hpp"
#include "evo/biology/founder.hpp"
#include <algorithm>
#include <cstdio>
#include <set>
#include <source_location>
#include <stdexcept>
#include <string>
// The rules-13 named mutation catalog (ADR-0179; Cellular_mutation_plan.md): stable identities,
// the eligible pool at birth, each kind of edit, gates and bounds.
namespace {
using namespace evo;
using namespace evo::biology;
void check(bool condition, std::source_location at = std::source_location::current()) {
if (!condition)
throw std::runtime_error("cellular catalog line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
Genome founder() {
const auto genome = primitive_founder(1, 1, primitive_founder_v6);
check(genome.has_value());
return genome.value_or(*primitive_founder(1, 1, primitive_founder_v6));
}
std::uint64_t next_id = 100;
std::optional<Genome> mutate(const Genome& plan, std::uint16_t id,
const CellularCatalogBounds& bounds = {},
const CellularLineageProgress& progress = {}) {
const auto* spec = cellular_mutation(id);
check(spec != nullptr);
if (spec == nullptr)
return std::nullopt;
const auto draft = apply_cellular_mutation(plan, *spec, cellular_default_tuning(*spec), bounds,
progress, ++next_id, 7);
if (!draft)
return std::nullopt;
const auto made = Genome::create(*draft);
check(made.has_value());
return made ? std::optional{*made} : std::nullopt;
}
std::uint32_t parameter(const Genome& genome, Capability kind) {
const auto value = genome.parameter(kind);
return value ? *value : 0;
}
CellularTraits traits(const Genome& genome) {
return genome.cellular_traits().value_or(CellularTraits{});
}
MovementArrangement arrangement(const Genome& genome) {
return genome.movement_arrangement().value_or(MovementArrangement{});
}
void identities_are_stable_and_unique() {
const auto all = cellular_catalog();
CHECK(all.size() == cellular_catalog_size);
std::set<std::uint16_t> ids;
std::set<std::string_view> keys;
for (const auto& spec : all) {
ids.insert(spec.id);
keys.insert(spec.key);
CHECK(!spec.name.empty() && !spec.pro.empty() && !spec.con.empty());
for (const auto& trigger : {spec.increase, spec.decrease})
CHECK(trigger.kind == CellularTriggerKind::event ||
trigger.kind == CellularTriggerKind::count || trigger.ticks != 0);
CHECK(cellular_mutation(spec.id) == &spec);
}
CHECK(ids.size() == cellular_catalog_size && *ids.begin() == 1 &&
*ids.rbegin() == cellular_catalog_size && keys.size() == cellular_catalog_size);
CHECK(cellular_mutation(0) == nullptr && cellular_mutation(50) == nullptr);
}
// 43 can be offered at birth (Cellular_mutation_plan.md section 9): every mutation but the
// three needing photosynthesis, Acquire Photosynthesis before any carbon is burned, Single Bud
// at litter 0, and Adhesion before five buds.
void the_pool_at_birth() {
const auto plan = founder();
CellularCatalogBounds bounds{};
bounds.carbon_for_light = 1'000'000;
std::set<std::uint16_t> closed;
for (const auto& spec : cellular_catalog())
if (!mutate(plan, spec.id, bounds))
closed.insert(spec.id);
CHECK(closed == std::set<std::uint16_t>{25, 26, 27, 28, 48, 49});
CHECK(mutate(plan, 25, bounds, {1'000'000, 0}).has_value());
CHECK(mutate(plan, 49, bounds, {0, 5}).has_value());
}
void each_kind_of_edit() {
const auto plan = founder();
const auto strong = mutate(plan, 1);
CHECK(strong && arrangement(*strong).output == 105 && arrangement(*strong).efficiency == 98);
CHECK(strong && strong.value().parents().size() == 1 &&
strong.value().parents()[0] == plan.id() &&
strong.value().schema_version() == genome_schema_v7);
const auto light = mutate(plan, 7);
CHECK(light && arrangement(*light).output == 95 && arrangement(*light).efficiency == 102);
const auto coasting = mutate(plan, 2);
CHECK(coasting && arrangement(*coasting).coasting == 105);
const auto gatherer = mutate(plan, 13);
CHECK(gatherer &&
traits(*gatherer).collection == std::array<std::uint16_t, 5>{100, 105, 99, 99, 99});
const auto metabolism = mutate(plan, 22);
CHECK(metabolism && traits(*metabolism).burn == std::array<std::uint16_t, 4>{99, 105, 99, 99});
const auto appetite = mutate(plan, 17);
CHECK(appetite && parameter(*appetite, Capability::ingestion) == 574'000 + 28'700 &&
traits(*appetite).upkeep_efficiency == 98);
const auto thorough = mutate(plan, 19);
CHECK(thorough && parameter(*thorough, Capability::energy_conversion) == 420 &&
traits(*thorough).collection[0] == 98);
const auto reinforced = mutate(plan, 29);
CHECK(reinforced && parameter(*reinforced, Capability::membrane) == 1'050);
const auto chamber = mutate(plan, 31);
CHECK(chamber && traits(*chamber).engulf_capacity == 105 &&
parameter(*chamber, Capability::membrane) == 980);
const auto larger = mutate(plan, 35);
CHECK(larger && parameter(*larger, Capability::target_size) == 8'400'000);
const auto thrifty = mutate(plan, 37);
CHECK(thrifty && traits(*thrifty).recipe == std::array<std::uint16_t, 4>{480, 158, 56, 306});
const auto buds = mutate(plan, 45);
CHECK(buds && parameter(*buds, Capability::reproduction) == 525);
const auto twins = mutate(plan, 47);
CHECK(twins && traits(*twins).litter == 25);
const auto single = twins ? mutate(*twins, 48) : std::nullopt;
CHECK(single && traits(*single).litter == 0);
const auto adhesion = mutate(plan, 49, {}, {0, 5});
CHECK(adhesion && traits(*adhesion).adhesion == 1 && !mutate(*adhesion, 49, {}, {0, 5}));
}
void photosynthesis_comes_and_goes() {
const auto plan = founder();
const auto producer = mutate(plan, 25);
CHECK(producer && parameter(*producer, Capability::photosynthesis) == 100 &&
traits(*producer).collection[0] == 98 && !mutate(*producer, 25));
if (!producer)
return;
const auto more = mutate(*producer, 26);
CHECK(more && parameter(*more, Capability::photosynthesis) == 105 &&
traits(*more).collection[0] == 96);
const auto less = mutate(*producer, 27);
CHECK(less && parameter(*less, Capability::photosynthesis) == 95 &&
traits(*less).collection[0] == 100);
const auto shed = mutate(*producer, 28);
CHECK(shed && !shed->carries(Capability::photosynthesis) && mutate(*shed, 25));
}
void bounds_and_switches() {
// Output climbs to the 1,000-point bound and no further; efficiency never below 25.
auto plan = founder();
for (int i = 0; i < 400; ++i) {
const auto next = mutate(plan, 1);
if (!next)
break;
plan = *next;
}
CHECK(arrangement(plan).output == 1'000 || arrangement(plan).efficiency < 27);
CHECK(!mutate(plan, 1));
CHECK(arrangement(plan).efficiency >= 25);
// A tighter bound closes it sooner.
CellularCatalogBounds tight{};
tight.max_points = 104;
CHECK(!mutate(founder(), 1, tight));
// A recipe never takes a channel below its minimum share: phosphorus starts at 50, so one
// step of 20 reaches 30 and a second would pass the minimum of 20.
const auto once = mutate(founder(), 39);
CHECK(once && traits(*once).recipe[2] == 30 && !mutate(*once, 39));
CellularCatalogBounds lenient{};
lenient.recipe_min_share = 10;
CHECK(once && mutate(*once, 39, lenient).has_value());
// A disabled mutation is never available.
const auto* spec = cellular_mutation(1);
CHECK(spec != nullptr);
if (spec == nullptr)
return;
auto off = cellular_default_tuning(*spec);
off.enabled = false;
CHECK(!apply_cellular_mutation(founder(), *spec, off, {}, {}, 999, 7));
// Tuned steps change the edit.
auto larger_step = cellular_default_tuning(*spec);
larger_step.primary_step = 10;
larger_step.paired_step = 0;
const auto tuned = apply_cellular_mutation(founder(), *spec, larger_step, {}, {}, 999, 7);
const auto tuned_arrangement =
tuned ? tuned->movement_arrangement.value_or(MovementArrangement{}) : MovementArrangement{};
CHECK(tuned_arrangement.output == 110 && tuned_arrangement.efficiency == 100);
// Earlier schemas have no catalog.
const auto older = primitive_founder(1, 1, primitive_founder_v5);
CHECK(older &&
!apply_cellular_mutation(*older, *spec, cellular_default_tuning(*spec), {}, {}, 999, 7));
}
} // namespace
int main() {
try {
identities_are_stable_and_unique();
the_pool_at_birth();
each_kind_of_edit();
photosynthesis_comes_and_goes();
bounds_and_switches();
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
return 1;
}
std::puts("PASS cellular mutation catalog");
return 0;
}