Files
aevolution/tests/development_tests.cpp
T
dingoandClaude Opus 5.5 4d17a0651c
Foundation / checks (checks) (push) Successful in 42m33s
Foundation / checks (deploy) (push) Successful in 4m11s
Foundation / checks (ecs) (push) Successful in 2m27s
Foundation / checks (network) (push) Successful in 19m23s
Foundation / checks (postgres) (push) Successful in 44m44s
Foundation / checks (tsan) (push) Successful in 9m53s
Foundation / checks (client) (push) Successful in 16m19s
Foundation / postgres-tools (push) Successful in 30s
Foundation / server-image (push) Successful in 3m14s
Foundation / image (push) Successful in 3m16s
Cellular balance rules 6 (ADR-0132, schema v41)
The playtest found mutation offers the arena could never pay for. A mutation's
matter cost is the real body difference (ADR-0118), so it cannot be discounted.
Rules 6 cut it honestly:
- genome schema 3 steps a third as far (1/300 of the range);
- development version 3 charges a third of the construction energy (13 instead of
  40 pJ per 1000 fg).

Founder 2 uses both. The cellular-v5 opening creates rules-6 regions on bundle v7,
which schema v41 admits. Existing lineages, regions and committed history are
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 20:53:31 -04:00

318 lines
16 KiB
C++

#include "evo/biology/development.hpp"
#include <iostream>
#include <limits>
#include <source_location>
#include <stdexcept>
#include <string>
#include <vector>
namespace {
using namespace evo::biology;
void check(bool condition, std::source_location location = std::source_location::current()) {
if (!condition) {
throw std::runtime_error("development line " + std::to_string(location.line()));
}
}
#define CHECK(expression) check(static_cast<bool>(expression))
// Target size 8e6 fg gives an exact cube root at full growth, so every derived
// value below is hand-calculable against ADR-0031's declared rules.
GenomeDraft draft() {
return {7,
1,
1,
{},
{{Capability::energy_conversion, 400, 0},
{Capability::membrane, 10, 0},
{Capability::target_size, 8'000'000, 0}}};
}
Genome build(const GenomeDraft& source) {
const auto genome = Genome::create(source);
check(genome.has_value());
return *genome;
}
// Development bounds growth by total mass, so a single channel carries the supply
// here; which elements a step consumes is settled in metabolism_tests.
constexpr evo::accounting::Inventory supply(std::uint64_t matter, std::uint64_t energy) {
return {{energy}, {evo::accounting::Femtograms{matter}, {}, {}, {}}};
}
constexpr EnvironmentState temperate{293'150, supply(1'000'000'000, 1'000'000'000)};
void hand_calculated_phenotype() {
const auto genome = build(draft());
const auto grown = derive_phenotype(genome, {1000, 0}, temperate);
CHECK(grown);
// 8e6 fg at density 1000 is 8000 um^3, an exact 20 um cube.
CHECK(grown->structural_mass == 8'000'000 && grown->extent == 20);
CHECK(grown->surface == 2400); // six 20x20 faces
CHECK(grown->membrane_mass == 28'800'000); // 2400 * 10 um * 1200
CHECK(grown->total_mass == 36'800'000);
CHECK(grown->maintenance == 73'600 + 86'400); // mass * 2 plus membrane * 3
CHECK(grown->conversion_per_thousand == 400);
CHECK(grown->movement_speed == 0 && grown->ingestion_rate == 0);
CHECK(grown->thermal_shortfall == 0);
// Half-built: 4e6 fg is 4000 um^3, and 15^3 = 3375 <= 4000 < 16^3.
const auto half = derive_phenotype(genome, {500, 0}, temperate);
CHECK(half && half->structural_mass == 4'000'000 && half->extent == 15);
CHECK(half->surface == 1350 && half->membrane_mass == 16'200'000);
CHECK(half->total_mass == 20'200'000);
}
void damage_is_not_genome() {
const auto genome = build(draft());
const auto before = genome.encode();
const auto intact = derive_phenotype(genome, {1000, 0}, temperate);
// A quarter lost: 6e6 fg is 6000 um^3, and 18^3 = 5832 <= 6000 < 19^3.
const auto hurt = derive_phenotype(genome, {1000, 250}, temperate);
CHECK(intact && hurt);
CHECK(hurt->structural_mass == 6'000'000 && hurt->extent == 18);
CHECK(hurt->surface == 1944 && hurt->membrane_mass == 23'328'000);
CHECK(hurt->total_mass == 29'328'000 && hurt->maintenance == 58'656 + 69'984);
CHECK(*hurt != *intact);
// The record a lineage inherits is untouched by what one organism lost.
CHECK(genome.encode() == before);
CHECK(genome.content_digest() == build(draft()).content_digest());
const auto repeated = derive_phenotype(genome, {1000, 250}, temperate);
CHECK(repeated && *repeated == *hurt);
}
void identical_inputs_give_identical_phenotypes() {
const auto genome = build(draft());
for (std::uint32_t realized = 0; realized <= 1000; realized += 125) {
for (std::uint32_t damage = 0; damage <= 1000; damage += 250) {
for (const auto temperature : {0U, 293'150U, 400'000U}) {
const EnvironmentState environment{temperature, supply(1'000'000, 1'000'000)};
const auto first = derive_phenotype(genome, {realized, damage}, environment);
const auto second = derive_phenotype(genome, {realized, damage}, environment);
CHECK(first && second && *first == *second);
// More growth never yields less structure at equal damage.
if (realized >= 125) {
const auto smaller =
derive_phenotype(genome, {realized - 125, damage}, environment);
CHECK(smaller && smaller->structural_mass <= first->structural_mass);
}
}
}
}
}
void capability_prerequisites() {
const auto bare = build(draft());
const auto without = derive_phenotype(bare, {1000, 0}, temperate);
CHECK(without && without->movement_speed == 0 && without->ingestion_rate == 0);
auto source = draft();
source.capabilities.push_back({Capability::movement, 250, 0});
source.capabilities.push_back({Capability::ingestion, 120'000, 0});
const auto capable = build(source);
const auto with = derive_phenotype(capable, {1000, 0}, temperate);
CHECK(with && with->movement_speed == 250);
CHECK(with->ingestion_rate == 4800); // 120000 * 2400 / 60000 reference surface
// Size is a real tradeoff: uptake scales with surface, maintenance with mass.
const auto smaller = derive_phenotype(capable, {500, 0}, temperate);
CHECK(smaller && smaller->ingestion_rate < with->ingestion_rate);
CHECK(smaller->maintenance < with->maintenance);
CHECK(smaller->movement_speed == with->movement_speed); // Speed is not size-scaled.
// Everything else about the two genomes is identical.
CHECK(with->structural_mass == without->structural_mass);
CHECK(with->maintenance == without->maintenance);
}
// ADR-0099: development version 1 reads genome rates per second at its 50 ms quantum.
// A schema 1 genome and its exact schema 2 translation develop identically, and a schema
// 2 rate that is not a whole number per tick rounds down (a capacity).
void schema_v2_rates_develop_identically() {
auto old = draft();
old.capabilities.push_back({Capability::movement, 250, 0});
old.capabilities.push_back({Capability::ingestion, 120'000, 0});
auto now = old;
now.schema_version = genome_schema_v2;
now.capabilities[3].parameter = 5'000; // 250 per tick, per second.
now.capabilities[4].parameter = 2'400'000; // 120000 per tick, per second.
for (const std::uint32_t realized : {250U, 500U, 1000U}) {
const auto a = derive_phenotype(build(old), {realized, 0}, temperate);
const auto b = derive_phenotype(build(now), {realized, 0}, temperate);
CHECK(a && b && *a == *b);
const auto pa = plan_development(build(old), {realized, 0}, temperate);
const auto pb = plan_development(build(now), {realized, 0}, temperate);
CHECK(pa && pb && *pa == *pb);
}
now.capabilities[3].parameter = 5'019; // 250.95 per tick.
const auto rounded = derive_phenotype(build(now), {1000, 0}, temperate);
CHECK(rounded && rounded->movement_speed == 250);
}
void thermal_shortfall_is_reported_not_enforced() {
auto source = draft();
source.capabilities.push_back({Capability::thermal_tolerance, 5'000, 0});
const auto tolerant = build(source);
const auto bare = build(draft());
for (const auto offset : {0U, 5'000U}) {
const EnvironmentState warm{293'150 + offset, {}};
const auto inside = derive_phenotype(tolerant, {1000, 0}, warm);
CHECK(inside && inside->thermal_shortfall == 0);
}
const EnvironmentState hot{293'150 + 8'000, {}};
const auto stressed = derive_phenotype(tolerant, {1000, 0}, hot);
CHECK(stressed && stressed->thermal_shortfall == 3'000);
const EnvironmentState cold{293'150 - 8'000, {}};
const auto chilled = derive_phenotype(tolerant, {1000, 0}, cold);
CHECK(chilled && chilled->thermal_shortfall == 3'000); // Symmetric about the reference.
// Without the capability every deviation is reported in full.
const auto exposed = derive_phenotype(bare, {1000, 0}, hot);
CHECK(exposed && exposed->thermal_shortfall == 8'000);
// Shortfall is a reported quantity: nothing else about the body changes.
CHECK(stressed->total_mass == exposed->total_mass);
CHECK(stressed->maintenance == exposed->maintenance);
}
void rule_versions_and_invalid_growth() {
CHECK(rules_for(1) && rules_for(2)); // Version 2: per second (ADR-0099).
// Version 3 (rules 6): version 2 with a third of the construction energy.
const auto v2 = rules_for(2);
const auto v3 = rules_for(3);
CHECK(v2 && v3 && v3->construction_per_kilogram == 13 && v2->construction_per_kilogram == 40 &&
v3->structure_density == v2->structure_density &&
v3->maintenance_per_kilogram == v2->maintenance_per_kilogram &&
development_rate_microseconds(3) == development_rate_microseconds(2));
for (const auto version : {0U, 4U, 99U}) {
const auto missing = rules_for(version);
CHECK(!missing && missing.error() == DevelopmentError::unsupported_rules);
}
auto source = draft();
source.development_version = 4;
const auto future = build(source);
const auto derived = derive_phenotype(future, {1000, 0}, temperate);
CHECK(!derived && derived.error() == DevelopmentError::unsupported_rules);
const auto planned = plan_development(future, {0, 0}, temperate);
CHECK(!planned && planned.error() == DevelopmentError::unsupported_rules);
const auto genome = build(draft());
for (const auto growth : {GrowthState{1001, 0}, GrowthState{0, 1001}}) {
const auto rejected = derive_phenotype(genome, growth, temperate);
CHECK(!rejected && rejected.error() == DevelopmentError::invalid_growth);
const auto refused = plan_development(genome, growth, temperate);
CHECK(!refused && refused.error() == DevelopmentError::invalid_growth);
}
}
void availability_bounds_do_not_wrap() {
const auto genome = build(draft());
// Four channels of 2^62 sum to exactly 2^64. A wrapped total reads as no
// material at all and refuses growth that is plainly funded.
constexpr std::uint64_t vast = 1ULL << 62U;
constexpr evo::accounting::Inventory enormous{
{std::numeric_limits<std::uint64_t>::max()},
{evo::accounting::Femtograms{vast}, evo::accounting::Femtograms{vast},
evo::accounting::Femtograms{vast}, evo::accounting::Femtograms{vast}}};
const EnvironmentState boundless{293'150, enormous};
const auto funded = plan_development(genome, {500, 0}, boundless);
CHECK(funded && funded->after == (GrowthState{550, 0}));
CHECK(funded->material_required == 2'632'000);
// An ordinary supply funds exactly the same step.
const auto ordinary = plan_development(genome, {500, 0}, temperate);
CHECK(ordinary && *ordinary == *funded);
// A single saturated channel is the other boundary.
constexpr evo::accounting::Inventory brimming{
{std::numeric_limits<std::uint64_t>::max()},
{evo::accounting::Femtograms{std::numeric_limits<std::uint64_t>::max()}, {}, {}, {}}};
const auto single = plan_development(genome, {500, 0}, {293'150, brimming});
CHECK(single && *single == *funded);
}
void plans_are_bounded_and_funded() {
const auto genome = build(draft());
// From half built, one step of 50 reaches 550: 4.4e6 fg is 4400 um^3 and
// 16^3 = 4096 <= 4400, so the extent grows from 15 to 16.
const auto funded = plan_development(genome, {500, 0}, temperate);
CHECK(funded);
CHECK(funded->after == (GrowthState{550, 0}));
CHECK(funded->material_required == 2'632'000); // 22832000 - 20200000
CHECK(funded->energy_required == 105'280); // material * 40 per 1000 fg
// One femtogram short of the full step buys exactly the largest step that fits.
const EnvironmentState tight{293'150, supply(2'631'999, 1'000'000'000)};
const auto reduced = plan_development(genome, {500, 0}, tight);
CHECK(reduced);
CHECK(reduced->after == (GrowthState{549, 0}));
CHECK(reduced->material_required == 2'624'000);
CHECK(reduced->material_required <= tight.available.matter[0].value);
// No material funds no growth, and nothing is granted.
const EnvironmentState barren{293'150, {}};
const auto refused = plan_development(genome, {500, 0}, barren);
CHECK(refused && refused->after == (GrowthState{500, 0}));
CHECK(refused->material_required == 0 && refused->energy_required == 0);
// A finished organism plans nothing further.
const auto complete = plan_development(genome, {1000, 0}, temperate);
CHECK(complete && complete->after == (GrowthState{1000, 0}));
CHECK(complete->material_required == 0);
// The last step is clamped to the target rather than overshooting it.
const auto final_step = plan_development(genome, {980, 0}, temperate);
CHECK(final_step && final_step->after == (GrowthState{1000, 0}));
// A plan never repairs damage.
const auto damaged = plan_development(genome, {500, 300}, temperate);
CHECK(damaged && damaged->after.damage_per_thousand == 300);
}
void flooring_consequences() {
// A body whose mass floors to nothing advances its counter for no material.
// This is the declared flooring, not a grant: no structure exists either way.
auto source = draft();
source.capabilities = {{Capability::energy_conversion, 1, 0},
{Capability::membrane, 1, 0},
{Capability::target_size, 1, 0}};
const auto tiny = build(source);
const EnvironmentState barren{293'150, {}};
const auto plan = plan_development(tiny, {500, 0}, barren);
CHECK(plan && plan->after == (GrowthState{550, 0}));
CHECK(plan->material_required == 0 && plan->energy_required == 0);
const auto derived = derive_phenotype(tiny, {550, 0}, barren);
CHECK(derived && derived->structural_mass == 0 && derived->total_mass == 0);
CHECK(derived->extent == 0 && derived->surface == 0 && derived->maintenance == 0);
// The smallest body that rounds to a whole cubic micrometre is still tiny.
source.capabilities = {{Capability::energy_conversion, 1, 0},
{Capability::membrane, 1, 0},
{Capability::target_size, 1000, 0}};
const auto unit = build(source);
const auto whole = derive_phenotype(unit, {1000, 0}, barren);
CHECK(whole && whole->structural_mass == 1000 && whole->extent == 1);
CHECK(whole->surface == 6 && whole->membrane_mass == 7200);
}
void extreme_sizes_are_exact() {
auto source = draft();
source.capabilities = {{Capability::energy_conversion, 1000, 0},
{Capability::membrane, 10'000, 0},
{Capability::target_size, 1'000'000'000, 0},
{Capability::ingestion, 1'000'000, 0}};
const auto largest = build(source);
const auto derived = derive_phenotype(largest, {1000, 0}, temperate);
CHECK(derived);
// 1e9 fg at density 1000 is 1e6 um^3, an exact 100 um cube.
CHECK(derived->structural_mass == 1'000'000'000 && derived->extent == 100);
CHECK(derived->surface == 60'000);
CHECK(derived->membrane_mass == 720'000'000'000); // 60000 * 10000 * 1200
CHECK(derived->total_mass == 721'000'000'000);
CHECK(derived->ingestion_rate == 1'000'000); // Exactly the reference surface.
CHECK(derived->maintenance == 1'442'000'000 + 2'160'000'000);
// Cube roots are exact on both sides of a boundary.
source.capabilities[2] = {Capability::target_size, 999'999'999, 0};
const auto under = derive_phenotype(build(source), {1000, 0}, temperate);
CHECK(under && under->extent == 99); // 999999 um^3 is one short of 100^3
}
} // namespace
int main() try {
hand_calculated_phenotype();
damage_is_not_genome();
identical_inputs_give_identical_phenotypes();
capability_prerequisites();
schema_v2_rates_develop_identically();
thermal_shortfall_is_reported_not_enforced();
rule_versions_and_invalid_growth();
availability_bounds_do_not_wrap();
plans_are_bounded_and_funded();
flooring_consequences();
extreme_sizes_are_exact();
std::cout << "PASS development rules, phenotype derivation and bounded plans\n";
return 0;
} catch (...) {
return 1;
}