Files
aevolution/tests/metabolism_tests.cpp
dingoandClaude Opus 5.5 c183021fd5
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 4m27s
Foundation / postgres-tools (push) Successful in 33s
Foundation / server-image (push) Successful in 4m14s
Foundation / image (push) Successful in 4m41s
feat(biology): genome schema 7 traits, founder 6 and development 7
Phase 1 of the cellular revamp build plan (ADR-0179). Genome schema 7 carries
twenty 16-bit cellular traits after the movement arrangement: density, upkeep
efficiency, growth speed, collection per channel (E/C/N/P/O), burn efficiency
per matter channel, engulf capacity, kill absorption, litter, the body recipe
(C/N/P/O per thousand, summing to 1000) and adhesion. Its membrane is held in
nanometres so it can change in 5% steps of the founder's 1 um. The largest
record still fits the 256-byte bound.

Development 7 and founder 6 serve rules 13: density sets how large a body is
for its mass, the recipe sets the body's composition (intake keeps the founder
shares), upkeep is an essential share no trait reduces plus the rest over upkeep
efficiency (region-declared BodyTuning, trial 25%), the producer speed penalty
is dropped, and the reproduction value is the bud's share of the body with the
construction discount fixed at the founder's level. Founder 6 builds exactly
founder 5's cell. Earlier schemas and development versions are unchanged.

The development and metabolism test mains now print the failing line. Refreshes
the DEV session window.

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

703 lines
36 KiB
C++

#include "evo/biology/founder.hpp"
#include "evo/processes/genome_strategy.hpp"
#include <iostream>
#include <limits>
#include <source_location>
#include <stdexcept>
#include <string>
#include <vector>
namespace {
using namespace evo::biology;
using namespace evo::accounting;
void check(bool condition, std::source_location location = std::source_location::current()) {
if (!condition) {
throw std::runtime_error("metabolism line " + std::to_string(location.line()));
}
}
#define CHECK(expression) check(static_cast<bool>(expression))
// The ADR-0031 fixture: an exact 20 micrometre cube at full growth, so every
// budget below is hand-calculable against the declared compositions.
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;
}
GenomeDraft capable() {
auto source = draft();
source.capabilities.push_back({Capability::movement, 250, 0});
source.capabilities.push_back({Capability::ingestion, 120'000, 0});
source.capabilities.push_back({Capability::reproduction, 300, 0});
return source;
}
// Every channel stocked alike, so availability never accidentally hides a
// per-element shortfall except where a test sets one deliberately.
constexpr Inventory pooled(std::uint64_t value) {
return {{value}, {Femtograms{value}, Femtograms{value}, Femtograms{value}, Femtograms{value}}};
}
constexpr EnvironmentState temperate{293'150, pooled(1'000'000'000)};
bool fits_within(const Inventory& needed, const Inventory& available) {
if (needed.energy.value > available.energy.value) {
return false;
}
for (std::size_t index = 0; index < 4; ++index) {
if (needed.matter[index].value > available.matter[index].value) {
return false;
}
}
return true;
}
std::uint64_t matter_total(const Inventory& inventory) {
std::uint64_t sum = 0;
for (const auto& mass : inventory.matter) {
sum += mass.value;
}
return sum;
}
void hand_calculated_budget() {
const auto genome = build(capable());
const auto budget = metabolic_budget(genome, {1000, 0}, temperate);
CHECK(budget);
// Structure 8e6 fg at 500/150/50/300, membrane 28.8e6 fg at 700/50/100/150.
CHECK(budget->body.matter[0].value == 24'160'000); // 4.0e6 + 20.16e6 carbon
CHECK(budget->body.matter[1].value == 2'640'000); // 1.2e6 + 1.44e6 nitrogen
CHECK(budget->body.matter[2].value == 3'280'000); // 0.4e6 + 2.88e6 phosphorus
CHECK(budget->body.matter[3].value == 6'720'000); // 2.4e6 + 4.32e6 other
CHECK(budget->body.energy.value == 0); // A body is matter, not energy.
// A split conserves the whole mass exactly, with nothing lost to rounding.
CHECK(matter_total(budget->body) == budget->phenotype.total_mass);
CHECK(budget->maintenance.value == 160'000);
CHECK(budget->movement.value == 184'000); // 36800 * 20 * 250 / 1000
CHECK(budget->demand.value == 344'000);
CHECK(budget->intake_limit.energy.value == 240'000); // 4800 fg at 50000 per 1000
CHECK(budget->intake_limit.matter[0].value == 2400); // 4800 fg by structure shares
CHECK(budget->intake_limit.matter[1].value == 720);
CHECK(budget->intake_limit.matter[2].value == 240);
CHECK(budget->intake_limit.matter[3].value == 1440);
CHECK(matter_total(budget->intake_limit) == budget->phenotype.ingestion_rate);
CHECK(budget->assimilation_per_thousand == 400);
// A parent divides the body it has, so the investment keeps its composition.
CHECK(budget->reproduction_investment.matter[0].value == 7'248'000);
CHECK(budget->reproduction_investment.matter[1].value == 792'000);
CHECK(budget->reproduction_investment.matter[2].value == 984'000);
CHECK(budget->reproduction_investment.matter[3].value == 2'016'000);
CHECK(matter_total(budget->reproduction_investment) == 11'040'000); // 30% of the body
CHECK(budget->reproduction_investment.energy.value == 441'600); // priced at 40 per 1000
// A finished organism has nothing left to build.
CHECK(matter_total(budget->construction.intake) == 0);
CHECK(matter_total(budget->construction.release) == 0);
CHECK(budget->construction.intake.energy.value == 0);
}
void construction_composes_each_compartment() {
const auto genome = build(capable());
const auto budget = metabolic_budget(genome, {500, 0}, temperate);
CHECK(budget);
// The step adds 400,000 fg of structure and 2,232,000 fg of membrane, each
// composed by its own shares rather than by one blended ratio.
CHECK(budget->construction.intake.matter[0].value == 200'000 + 1'562'400);
CHECK(budget->construction.intake.matter[1].value == 60'000 + 111'600);
CHECK(budget->construction.intake.matter[2].value == 20'000 + 223'200);
CHECK(budget->construction.intake.matter[3].value == 120'000 + 334'800);
CHECK(matter_total(budget->construction.intake) == 2'632'000);
CHECK(matter_total(budget->construction.release) == 0); // Nothing floors here.
CHECK(budget->construction.intake.energy.value == 105'280);
// What a plan reports as material is the net of the transition.
const auto plan = plan_development(genome, {500, 0}, temperate);
CHECK(plan && plan->material_required == matter_total(budget->construction.intake) -
matter_total(budget->construction.release));
CHECK(plan->energy_required == budget->construction.intake.energy.value);
CHECK(budget->construction.after == plan->after);
// Unaffordable growth leaves nothing to move.
const EnvironmentState barren{293'150, pooled(0)};
const auto starved = metabolic_budget(genome, {500, 0}, barren);
CHECK(starved && matter_total(starved->construction.intake) == 0);
CHECK(matter_total(starved->construction.release) == 0);
CHECK(starved->construction.intake.energy.value == 0);
}
void construction_reproduces_the_derived_body() {
// Composing the mass difference would fund C25 N7 P2 other16 here, leaving
// nitrogen and phosphorus one short and other two over, so spending it could
// not produce the body the same rules derive.
auto source = draft();
source.capabilities[2].parameter = 1001; // target size
const auto genome = build(source);
const auto before = metabolic_budget(genome, {10, 0}, temperate);
CHECK(before);
CHECK(before->body.matter[0].value == 5 && before->body.matter[1].value == 1);
CHECK(before->body.matter[2].value == 0 && before->body.matter[3].value == 4);
CHECK(before->construction.intake.matter[0].value == 25);
CHECK(before->construction.intake.matter[1].value == 8);
CHECK(before->construction.intake.matter[2].value == 3);
CHECK(before->construction.intake.matter[3].value == 14);
CHECK(matter_total(before->construction.intake) == 50);
CHECK(matter_total(before->construction.release) == 0);
const auto after = metabolic_budget(genome, {60, 0}, temperate);
CHECK(after);
CHECK(after->body.matter[0].value == 30 && after->body.matter[1].value == 9);
CHECK(after->body.matter[2].value == 3 && after->body.matter[3].value == 18);
for (std::size_t index = 0; index < 4; ++index) {
CHECK(before->body.matter[index].value + before->construction.intake.matter[index].value -
before->construction.release.matter[index].value ==
after->body.matter[index].value);
}
// A step can also free a femtogram as the declared shares rebalance. At a
// target of 20 fg a full step moves the body from 13 to 14 fg: carbon and
// nitrogen each gain one while other falls from 6 to 5.
source.capabilities[2].parameter = 20;
const auto small = build(source);
const auto crawling = metabolic_budget(small, {650, 0}, temperate);
CHECK(crawling && crawling->phenotype.structural_mass == 13);
CHECK(crawling->body.matter[3].value == 6);
CHECK(crawling->construction.after == (GrowthState{700, 0}));
CHECK(crawling->construction.intake.matter[0].value == 1);
CHECK(crawling->construction.intake.matter[1].value == 1);
CHECK(crawling->construction.intake.matter[2].value == 0);
CHECK(crawling->construction.release.matter[3].value == 1);
const auto stepped = metabolic_budget(small, {700, 0}, temperate);
CHECK(stepped && stepped->phenotype.structural_mass == 14);
CHECK(stepped->body.matter[3].value == 5);
for (std::size_t index = 0; index < 4; ++index) {
CHECK(crawling->body.matter[index].value +
crawling->construction.intake.matter[index].value -
crawling->construction.release.matter[index].value ==
stepped->body.matter[index].value);
}
}
void released_matter_never_funds_another_element() {
// The 13 to 14 fg step again: net growth is one femtogram, but it acquires a
// carbon and a nitrogen while freeing one of other. Released other is neither
// carbon nor nitrogen, so a single femtogram cannot fund it whatever channel
// that femtogram sits in.
auto source = draft();
source.capabilities[2].parameter = 20;
const auto genome = build(source);
for (std::size_t channel = 0; channel < 4; ++channel) {
Inventory single{{1'000'000}, {}};
single.matter[channel].value = 1; // One femtogram, and only one.
const EnvironmentState scarce{293'150, single};
// The mass-only plan is what would be fooled: it sees a net of one
// femtogram against a pool of one and admits the step.
const auto plan = plan_development(genome, {650, 0}, scarce);
CHECK(plan && plan->after == (GrowthState{700, 0}) && plan->material_required == 1);
const auto budget = metabolic_budget(genome, {650, 0}, scarce);
CHECK(budget);
CHECK(budget->construction.after != (GrowthState{700, 0})); // Not funded.
// Nothing is acquired at all: the steps that remain change no mass.
CHECK(matter_total(budget->construction.intake) == 0);
CHECK(matter_total(budget->construction.release) == 0);
CHECK(fits_within(budget->construction.intake, single));
const auto reached = metabolic_budget(genome, budget->construction.after, scarce);
CHECK(reached && reached->phenotype.structural_mass == 13);
}
// A carbon and a nitrogen together are exactly what the step needs.
Inventory exact{{1'000'000}, {}};
exact.matter[0].value = 1;
exact.matter[1].value = 1;
const auto precise = metabolic_budget(genome, {650, 0}, {293'150, exact});
CHECK(precise && precise->construction.after == (GrowthState{700, 0}));
CHECK(precise->construction.intake.matter[0].value == 1);
CHECK(precise->construction.intake.matter[1].value == 1);
CHECK(precise->construction.release.matter[3].value == 1);
}
void enormous_availability_still_funds_growth() {
const auto genome = build(capable());
// Budgeting reads the same availability bound, so a wrapped total would
// refuse this step too. Four channels of 2^62 sum to exactly 2^64.
constexpr std::uint64_t vast = 1ULL << 62U;
const Inventory enormous{
{std::numeric_limits<std::uint64_t>::max()},
{Femtograms{vast}, Femtograms{vast}, Femtograms{vast}, Femtograms{vast}}};
const auto budget = metabolic_budget(genome, {500, 0}, {293'150, enormous});
const auto ordinary = metabolic_budget(genome, {500, 0}, temperate);
CHECK(budget && ordinary);
CHECK(budget->construction.after == (GrowthState{550, 0}));
CHECK(budget->construction == ordinary->construction);
CHECK(matter_total(budget->construction.intake) == 2'632'000);
}
void every_transition_is_affordable_channel_by_channel() {
auto source = capable();
for (const std::uint32_t target : {1U, 999U, 1001U, 9'999U, 123'457U, 8'000'000U}) {
source.capabilities[2].parameter = target;
const auto genome = build(source);
for (const std::uint32_t realized : {0U, 1U, 13U, 333U, 500U, 950U, 1000U}) {
for (const std::uint64_t stock : {0ULL, 1ULL, 7ULL, 1'000'000ULL}) {
const EnvironmentState environment{293'150, pooled(stock)};
const auto budget = metabolic_budget(genome, {realized, 0}, environment);
CHECK(budget);
// Whatever a step reports, every element of it must be there.
CHECK(fits_within(budget->construction.intake, environment.available));
// A funded step never exceeds what the mass-only plan proposed.
const auto plan = plan_development(genome, {realized, 0}, environment);
CHECK(plan && budget->construction.after.realized_per_thousand <=
plan->after.realized_per_thousand);
CHECK(budget->construction.after.damage_per_thousand == 0);
}
}
}
}
void every_transition_conserves_each_channel() {
auto source = capable();
for (const std::uint32_t target : {1U, 999U, 1001U, 9'999U, 123'457U, 8'000'000U}) {
source.capabilities[2].parameter = target;
const auto genome = build(source);
for (const std::uint32_t realized : {0U, 1U, 13U, 333U, 500U, 950U, 1000U}) {
for (const std::uint64_t material : {0ULL, 1ULL, 7ULL, 1'000'000'000ULL}) {
const EnvironmentState environment{293'150, pooled(material)};
const auto budget = metabolic_budget(genome, {realized, 0}, environment);
const auto plan = plan_development(genome, {realized, 0}, environment);
CHECK(budget && plan);
const auto reached =
metabolic_budget(genome, budget->construction.after, environment);
CHECK(reached);
// Spending the budget must reproduce the derived body exactly, on
// every channel, not merely in total.
for (std::size_t index = 0; index < 4; ++index) {
CHECK(budget->body.matter[index].value +
budget->construction.intake.matter[index].value -
budget->construction.release.matter[index].value ==
reached->body.matter[index].value);
}
// The net of the transition is what the plan charges material for.
CHECK(matter_total(budget->construction.intake) >=
matter_total(budget->construction.release));
CHECK(matter_total(budget->construction.intake) -
matter_total(budget->construction.release) <=
plan->material_required);
}
}
}
}
void repeated_growth_conserves_each_channel() {
auto source = capable();
source.capabilities[2].parameter = 123'457;
const auto genome = build(source);
// Walk a body from nothing to its target one funded step at a time, checking
// every channel across the whole history rather than only one transition.
GrowthState growth{0, 0};
Inventory acquired{}, freed{};
const auto opening = metabolic_budget(genome, growth, temperate);
CHECK(opening);
for (int step = 0; step < 40 && growth.realized_per_thousand < 1000; ++step) {
const auto budget = metabolic_budget(genome, growth, temperate);
CHECK(budget &&
budget->construction.after.realized_per_thousand > growth.realized_per_thousand);
for (std::size_t index = 0; index < 4; ++index) {
acquired.matter[index].value += budget->construction.intake.matter[index].value;
freed.matter[index].value += budget->construction.release.matter[index].value;
}
growth = budget->construction.after;
}
CHECK(growth.realized_per_thousand == 1000);
const auto finished = metabolic_budget(genome, growth, temperate);
CHECK(finished);
for (std::size_t index = 0; index < 4; ++index) {
CHECK(opening->body.matter[index].value + acquired.matter[index].value -
freed.matter[index].value ==
finished->body.matter[index].value);
}
}
void thicker_membrane_costs_matter_and_maintenance() {
auto source = capable();
const auto thin = metabolic_budget(build(source), {1000, 0}, temperate);
source.capabilities[1].parameter = 20; // membrane, twice as thick
const auto thick = metabolic_budget(build(source), {1000, 0}, temperate);
CHECK(thin && thick);
// Costs more matter to build and more energy to hold.
CHECK(matter_total(thick->body) == 65'600'000);
CHECK(matter_total(thick->body) > matter_total(thin->body));
CHECK(thick->maintenance.value == 304'000); // 65600*2 + 57600*3
CHECK(thick->maintenance.value > thin->maintenance.value);
// Membrane is the carbon-rich compartment, so composition shifts, not just mass.
CHECK(thick->body.matter[0].value == 44'320'000);
const auto thin_carbon_share = thin->body.matter[0].value * 1000 / matter_total(thin->body);
const auto thick_carbon_share = thick->body.matter[0].value * 1000 / matter_total(thick->body);
CHECK(thick_carbon_share > thin_carbon_share);
// Hauling the heavier body costs more, so a thick membrane is paid for twice.
CHECK(thick->movement.value > thin->movement.value);
// These budgets are untouched: nothing here is a multiplier over a total.
CHECK(thick->intake_limit == thin->intake_limit);
CHECK(thick->phenotype.movement_speed == thin->phenotype.movement_speed);
CHECK(thick->assimilation_per_thousand == thin->assimilation_per_thousand);
CHECK(thick->phenotype.structural_mass == thin->phenotype.structural_mass);
// Doubling one parameter moves the affected budgets by different ratios.
const auto mass_ratio = matter_total(thick->body) * 1000 / matter_total(thin->body);
const auto maintenance_ratio = thick->maintenance.value * 1000 / thin->maintenance.value;
CHECK(mass_ratio != maintenance_ratio);
}
void size_moves_several_budgets_together() {
const auto genome = build(capable());
const auto small = metabolic_budget(genome, {500, 0}, temperate);
const auto large = metabolic_budget(genome, {1000, 0}, temperate);
CHECK(small && large);
CHECK(matter_total(large->body) > matter_total(small->body));
CHECK(large->maintenance.value > small->maintenance.value);
CHECK(large->movement.value > small->movement.value);
CHECK(matter_total(large->intake_limit) > matter_total(small->intake_limit));
CHECK(matter_total(large->reproduction_investment) >
matter_total(small->reproduction_investment));
// Uptake rises with surface while maintenance rises with mass, so the two do
// not move together: size is a genuine tradeoff, not a uniform gain.
const auto uptake_ratio =
matter_total(large->intake_limit) * 1000 / matter_total(small->intake_limit);
const auto maintenance_ratio = large->maintenance.value * 1000 / small->maintenance.value;
CHECK(uptake_ratio != maintenance_ratio);
}
void absent_capabilities_cost_nothing() {
const auto bare = build(draft());
const auto budget = metabolic_budget(bare, {1000, 0}, temperate);
CHECK(budget);
CHECK(budget->movement.value == 0); // No movement capability.
CHECK(budget->demand.value == budget->maintenance.value);
CHECK(matter_total(budget->intake_limit) == 0); // No ingestion capability.
CHECK(budget->intake_limit.energy.value == 0);
CHECK(matter_total(budget->reproduction_investment) == 0); // No reproduction capability.
CHECK(budget->reproduction_investment.energy.value == 0);
// A body still costs maintenance whatever it cannot do.
CHECK(budget->maintenance.value == 160'000);
CHECK(matter_total(budget->body) == 36'800'000);
}
void rules_fail_closed() {
CHECK(metabolic_rules_for(1));
// Version 3 (rules 6) keeps version 2's metabolism; 4 is unknown.
const auto m2 = metabolic_rules_for(2);
const auto m3 = metabolic_rules_for(3);
CHECK(m2 && m3 && m3->movement_per_kilogram == m2->movement_per_kilogram &&
m3->intake_energy_per_kilogram == m2->intake_energy_per_kilogram);
for (const auto version : {0U, 8U, 99U}) {
const auto missing = metabolic_rules_for(version);
CHECK(!missing && missing.error() == DevelopmentError::unsupported_rules);
}
auto source = draft();
source.development_version = 8; // Version 7 exists since rules 13.
const auto future = build(source);
const auto refused = metabolic_budget(future, {1000, 0}, temperate);
CHECK(!refused && refused.error() == DevelopmentError::unsupported_rules);
const auto genome = build(draft());
const auto invalid = metabolic_budget(genome, {1001, 0}, temperate);
CHECK(!invalid && invalid.error() == DevelopmentError::invalid_growth);
// Both compositions are declared, and both sum to the whole.
const auto rules = metabolic_rules_for(1);
std::uint32_t structure_total{}, membrane_total{};
for (std::size_t index = 0; index < 4; ++index) {
structure_total += rules->structure[index];
membrane_total += rules->membrane[index];
}
CHECK(structure_total == 1000 && membrane_total == 1000);
}
void splits_conserve_at_awkward_masses() {
// Masses chosen so every share floors, forcing the last channel to absorb it.
auto source = draft();
for (const std::uint32_t target : {1001U, 9'999U, 123'457U, 999'999'999U}) {
source.capabilities[2].parameter = target;
const auto genome = build(source);
for (const std::uint32_t realized : {1U, 333U, 999U, 1000U}) {
const auto budget = metabolic_budget(genome, {realized, 0}, temperate);
CHECK(budget);
// Whatever the rounding, the parts always sum back to the whole.
CHECK(matter_total(budget->body) == budget->phenotype.total_mass);
CHECK(matter_total(budget->intake_limit) == budget->phenotype.ingestion_rate);
}
}
}
void a_genome_drives_a_feeding_tick() {
using namespace evo::processes;
const auto genome = build(capable());
const auto opening =
Inventory{{0}, {Femtograms{0}, Femtograms{0}, Femtograms{0}, Femtograms{0}}};
const auto consumer = consumer_for(genome, {1000, 0}, temperate, opening);
CHECK(consumer);
const auto budget = metabolic_budget(genome, {1000, 0}, temperate);
CHECK(budget && consumer->intake_limit == budget->intake_limit);
CHECK(consumer->assimilation_per_thousand == 400);
CHECK(consumer->opening == opening); // The caller's, never invented.
// The derived cap drives a real community tick and the ledger still balances.
const ProducerConfig plants{
{{1'000'000}, 1},
{2, 1},
1000,
{1},
{Femtograms{4000}, Femtograms{4000}, Femtograms{4000}, Femtograms{4000}}};
const std::array configs{*consumer};
auto community = FeedingCommunity::create(
plants, {10'000'000},
{Femtograms{100'000}, Femtograms{100'000}, Femtograms{100'000}, Femtograms{100'000}},
configs);
CHECK(community);
Inventory total{};
for (const auto& balance : community->balances()) {
total.energy.value += balance.energy.value;
for (std::size_t index = 0; index < 4; ++index) {
total.matter[index].value += balance.matter[index].value;
}
}
const std::array meal{FeedIntent{
1, 2, 1, {{100}, {Femtograms{2000}, Femtograms{720}, Femtograms{240}, Femtograms{1440}}}}};
const auto receipts = community->advance(evo::Tick{1}, meal);
CHECK(receipts && receipts->size() == 1);
Inventory after{};
for (const auto& balance : community->balances()) {
after.energy.value += balance.energy.value;
for (std::size_t index = 0; index < 4; ++index) {
after.matter[index].value += balance.matter[index].value;
}
}
CHECK(after == total); // Conserved exactly, as every M2 tick must be.
// A bite past the genome's derived cap is refused by the existing contract.
const std::array excessive{FeedIntent{
1, 2, 1, {{100}, {Femtograms{2401}, Femtograms{0}, Femtograms{0}, Femtograms{0}}}}};
const auto denied = community->advance(evo::Tick{2}, excessive);
CHECK(!denied && denied.error() == FeedingError::intake_limit);
}
} // namespace
// Development version 2 (ADR-0099) and the primitive founder (ADR-0098 decision 7): rates
// per second, and a first bud fundable in about 5, 10 and 15 minutes feeding all, half
// and a third of the time while moving at full speed throughout. Tunable targets, so
// the bounds are the decision's range, not exact values.
void development_v2_and_the_first_bud() {
const auto founder = primitive_founder(1, 1);
CHECK(founder && founder->schema_version() == 2 && founder->development_version() == 2);
CHECK(!primitive_founder(0, 1));
const auto budget = metabolic_budget(*founder, {1000, 0}, temperate);
CHECK(budget);
const auto& p = budget->phenotype;
CHECK(budget->rate_microseconds == 1'000'000 && p.extent == 20 && p.surface == 2400);
CHECK(p.membrane_mass == 2'880'000 && p.total_mass == 10'880'000);
CHECK(p.maintenance == 108'800 + 43'200); // 10 and 15 pJ/s per 1000 fg.
CHECK(p.movement_speed == 200 && p.ingestion_rate == 22'960); // 574000 * 2400 / 60000.
CHECK(budget->movement.value == 108'800); // 10880 * 50 * 200 / 1000.
const auto at = [&](std::uint32_t share) { return bud_funding_seconds(*budget, share); };
const auto funded_all = at(1000), funded_half = at(500), funded_third = at(333);
CHECK(funded_all && funded_half && funded_third);
const auto all = funded_all.value_or(0), half = funded_half.value_or(0),
third = funded_third.value_or(0);
std::cout << "first bud seconds: all=" << all << " half=" << half << " third=" << third << "\n";
CHECK(all >= 270 && all <= 330);
CHECK(half >= 540 && half <= 660);
CHECK(third >= 810 && third <= 990);
CHECK(all < half && half < third);
CHECK(!at(100)); // Feeding a tenth of the time while always moving never funds a bud.
CHECK(!at(0) && !at(1001));
// Version 1 rates stay per 50 ms tick, and the M2 fixtures accept only version 1.
const auto v1 = metabolic_budget(build(capable()), {1000, 0}, temperate);
CHECK(v1 && v1->rate_microseconds == 50'000);
CHECK(!evo::processes::consumer_for(*founder, {1000, 0}, temperate, {}));
CHECK(evo::processes::consumer_for(build(capable()), {1000, 0}, temperate, {}));
// Without the reproduction capability there is nothing to fund.
auto plain = draft();
plain.schema_version = 2;
plain.development_version = 2;
CHECK(!bud_funding_seconds(*metabolic_budget(build(plain), {1000, 0}, temperate), 1000));
}
// 2026-10-15 balance: founder 2 (rules 6) is schema 3 and development 3. Its body, upkeep
// and reserve equal founder 1's; only construction energy falls, to 13/40 of it.
void founder_v2_builds_the_same_cell_for_less_energy() {
const auto v1 = primitive_founder(1, 1, primitive_founder_v1);
const auto v2 = primitive_founder(1, 1, primitive_founder_v2);
CHECK(v1 && v2 && v2->schema_version() == 3 && v2->development_version() == 3);
const auto b1 = metabolic_budget(*v1, {1000, 0}, temperate);
const auto b2 = metabolic_budget(*v2, {1000, 0}, temperate);
CHECK(b1 && b2 && b1->body == b2->body &&
b1->phenotype.maintenance == b2->phenotype.maintenance &&
b1->phenotype.extent == b2->phenotype.extent);
CHECK(b2->reproduction_investment.energy.value * 40 <=
b1->reproduction_investment.energy.value * 13 + 40);
const auto e1 = primitive_endowment(primitive_founder_v1);
const auto e2 = primitive_endowment(primitive_founder_v2);
CHECK(e1 && e2 && e1->body == e2->body && e1->reserve == e2->reserve &&
e2->construction.value < e1->construction.value &&
e2->construction.value * 40 <= e1->construction.value * 13 + 40);
CHECK(*primitive_endowment() == *e1); // The default stays founder 1.
}
// ADR-0137 (development 4): a bud is always half the body, and reproduction takes value /
// 2000 off its construction energy; the matter is never discounted.
void founder_v3_buds_more_cheaply_with_skill() {
const auto v2 = primitive_founder(1, 1, primitive_founder_v2);
const auto v3 = primitive_founder(1, 1, primitive_founder_v3);
CHECK(v2 && v3 && v3->schema_version() == 4 && v3->development_version() == 4);
const auto b2 = metabolic_budget(*v2, {1000, 0}, temperate);
const auto b3 = metabolic_budget(*v3, {1000, 0}, temperate);
CHECK(b2 && b3 && b2->body == b3->body && b2->phenotype == b3->phenotype);
const auto full = b2->reproduction_investment.energy.value; // Founders invest 500.
CHECK(b3->reproduction_investment.matter == b2->reproduction_investment.matter);
CHECK(b3->reproduction_investment.energy.value == full - full / 4);
const auto with_skill = [&](std::uint32_t reproduction) {
GenomeDraft draft;
draft.schema_version = genome_schema_v4;
draft.id = 2;
draft.origin_event = 1;
draft.development_version = 4;
for (const auto& record : v3->capabilities())
draft.capabilities.push_back(record);
for (auto& record : draft.capabilities)
if (record.kind == Capability::reproduction)
record.parameter = reproduction;
const auto genome = Genome::create(draft);
CHECK(genome.has_value());
const auto budget = metabolic_budget(genome.value_or(*v3), {1000, 0}, temperate);
CHECK(budget.has_value());
return budget.value_or(*b3).reproduction_investment;
};
// The bud stays half the body at any skill; full skill halves its energy.
CHECK(with_skill(100).matter == b3->reproduction_investment.matter);
CHECK(with_skill(1000).matter == b3->reproduction_investment.matter);
CHECK(with_skill(1000).energy.value == full - full / 2);
CHECK(with_skill(1).energy.value > with_skill(1000).energy.value);
}
// ADR-0179 (development 7, founder 6): the founder's traits build founder 5's cell; density,
// upkeep efficiency, the recipe and the bud share change the body as the plans say.
Genome founder6_with(auto edit, std::uint32_t photosynthesis = 0) {
const auto base = primitive_founder(1, 1, primitive_founder_v6);
CHECK(base.has_value());
GenomeDraft draft;
draft.schema_version = genome_schema_v7;
draft.id = 2;
draft.origin_event = 1;
draft.development_version = 7;
for (const auto& record : base->capabilities())
draft.capabilities.push_back(record);
if (photosynthesis != 0)
draft.capabilities.push_back({Capability::photosynthesis, photosynthesis, 0});
draft.movement_arrangement = base->movement_arrangement();
draft.cellular_traits = base->cellular_traits();
edit(draft);
const auto genome = Genome::create(draft);
CHECK(genome.has_value());
return genome.value_or(*base);
}
void founder_v6_builds_founder_v5s_cell() {
const auto v5 = primitive_founder(1, 1, primitive_founder_v5);
const auto v6 = primitive_founder(1, 1, primitive_founder_v6);
CHECK(v5 && v6 && v6->schema_version() == 7 && v6->development_version() == 7);
CHECK(*v6->parameter(Capability::membrane) == 1'000); // Nanometres: the same 1 um.
const auto b5 = metabolic_budget(*v5, {1000, 0}, temperate);
const auto b6 = metabolic_budget(*v6, {1000, 0}, temperate);
CHECK(b5 && b6 && b5->body == b6->body && b5->phenotype == b6->phenotype);
CHECK(b5->intake_limit == b6->intake_limit && b5->movement == b6->movement);
CHECK(b5->reproduction_investment == b6->reproduction_investment);
const auto e5 = primitive_endowment(primitive_founder_v5);
const auto e6 = primitive_endowment(primitive_founder_v6);
CHECK(e5 && e6 && *e5 == *e6);
}
void development_v7_traits_shape_the_body() {
const auto founder = founder6_with([](GenomeDraft&) {});
const auto base = metabolic_budget(founder, {1000, 0}, temperate);
CHECK(base.has_value());
const auto budget_of = [&](const Genome& genome, BodyTuning tuning = {}) {
const auto budget = metabolic_budget(genome, {1000, 0}, temperate, 1000, tuning);
CHECK(budget.has_value());
return budget.value_or(*base);
};
// Denser tissue packs the same structure smaller: less surface, membrane and upkeep.
const auto dense =
budget_of(founder6_with([](GenomeDraft& d) { d.cellular_traits->density = 125; }));
CHECK(dense.phenotype.structural_mass == base->phenotype.structural_mass);
CHECK(dense.phenotype.extent < base->phenotype.extent);
CHECK(dense.phenotype.membrane_mass < base->phenotype.membrane_mass);
CHECK(dense.phenotype.maintenance < base->phenotype.maintenance);
const auto light =
budget_of(founder6_with([](GenomeDraft& d) { d.cellular_traits->density = 80; }));
CHECK(light.phenotype.extent > base->phenotype.extent &&
light.phenotype.ingestion_rate > base->phenotype.ingestion_rate);
// Upkeep efficiency divides only the adjustable part: never below the essential floor.
const auto upkeep = base->phenotype.maintenance;
const auto efficient = [&](std::uint16_t value) {
return budget_of(founder6_with([value](GenomeDraft& d) {
d.cellular_traits->upkeep_efficiency = value;
}))
.phenotype.maintenance;
};
CHECK(efficient(200) < upkeep && efficient(1'000) < efficient(200));
CHECK(efficient(1'000) >= upkeep / 4 && efficient(50) > upkeep);
const auto all_essential = budget_of(
founder6_with([](GenomeDraft& d) { d.cellular_traits->upkeep_efficiency = 1'000; }),
BodyTuning{1000});
CHECK(all_essential.phenotype.maintenance == upkeep);
// The recipe changes what the body is made of, not how much there is or how it feeds.
const auto thrifty = budget_of(
founder6_with([](GenomeDraft& d) { d.cellular_traits->recipe = {480, 157, 57, 306}; }));
CHECK(thrifty.body.matter[0] < base->body.matter[0] &&
thrifty.body.matter[1] > base->body.matter[1]);
std::uint64_t before{}, after{};
for (std::size_t c = 0; c < 4; ++c) {
before += base->body.matter[c].value;
after += thrifty.body.matter[c].value;
}
CHECK(before == after && thrifty.intake_limit == base->intake_limit);
// Version 7 drops the producer speed penalty; the organelles still add mass.
const auto producer = budget_of(founder6_with([](GenomeDraft&) {}, 100));
CHECK(producer.phenotype.movement_speed == base->phenotype.movement_speed);
CHECK(producer.phenotype.total_mass > base->phenotype.total_mass);
// The reproduction value is the bud's share of the body; the discount stays fixed.
const auto bigger = budget_of(founder6_with([](GenomeDraft& d) {
for (auto& record : d.capabilities)
if (record.kind == Capability::reproduction)
record.parameter = 600;
}));
std::uint64_t half{}, sixty{};
for (std::size_t c = 0; c < 4; ++c) {
half += base->reproduction_investment.matter[c].value;
sixty += bigger.reproduction_investment.matter[c].value;
}
CHECK(sixty > half && sixty * 5 <= half * 6 + 8);
CHECK(bigger.reproduction_investment.energy.value * 5 <=
base->reproduction_investment.energy.value * 6 + 8);
// A version-7 genome needs its traits, and a bad tuning share fails closed.
CHECK(!metabolic_budget(founder, {1000, 0}, temperate, 1000, BodyTuning{1001}));
}
int main() try {
hand_calculated_budget();
construction_composes_each_compartment();
construction_reproduces_the_derived_body();
released_matter_never_funds_another_element();
enormous_availability_still_funds_growth();
every_transition_is_affordable_channel_by_channel();
every_transition_conserves_each_channel();
repeated_growth_conserves_each_channel();
thicker_membrane_costs_matter_and_maintenance();
size_moves_several_budgets_together();
absent_capabilities_cost_nothing();
rules_fail_closed();
splits_conserve_at_awkward_masses();
a_genome_drives_a_feeding_tick();
development_v2_and_the_first_bud();
founder_v2_builds_the_same_cell_for_less_energy();
founder_v3_buds_more_cheaply_with_skill();
founder_v6_builds_founder_v5s_cell();
development_v7_traits_shape_the_body();
std::cout << "PASS metabolic budgets, composition splits and genome-driven feeding\n";
return 0;
} catch (const std::exception& e) {
std::cerr << e.what() << '\n';
return 1;
} catch (...) {
return 1;
}