Give capability a cost without giving it a multiplier

M3.3 turns a phenotype into the budgets a ledger can hold: body
composition, maintenance, movement, the next construction step, the
per-tick intake limit and the reproduction investment. metabolism is the
single place the genome's description of capability becomes accounting
quantities, so the record itself stays free of those types and M3.1's
boundary is crossed once, explicitly.

The named risk is reintroducing RPG modifiers, where a capability
parameter becomes a flat percentage over every budget. Each budget is
therefore derived from a different phenotype quantity: maintenance from
mass plus membrane upkeep, movement from mass and speed, intake from
surface, construction from the mass a growth step adds, investment from
the body a parent already has. One parameter moves some budgets and not
others, by different amounts. Doubling membrane thickness costs more
matter to build, more energy to hold and more to haul, while leaving
uptake, speed, assimilation and structural mass identical, and the mass
and maintenance ratios differ, so no single multiplier could produce it.
The test asserts that non-uniformity rather than describing it.

Composition is declared per compartment, and membrane is the carbon-rich
one, so a thicker membrane changes what a body is made of and not only
how much. A split assigns the first three channels by share and gives the
remainder to the last, so parts always sum back to the whole and a
derived body can be spent through the ledger without a rounding leak.
That is checked at four awkward masses where every share floors.

processes::consumer_for is the first path from a genome into the M2
fixtures. Opening inventory stays the caller's: a genome says what an
organism can do, never what it holds, and a derived config that invented
an opening balance would create matter. A derived consumer is shown
driving a real feeding tick with the ledger conserving exactly, and a
bite one femtogram past the derived cap refused by the M2.3 contract.

Every budget is asserted as a hand-calculated literal. The constants are
chosen so budgets are of comparable magnitude, which is legibility for
tests and not tuning. No existing fixture reads genomes, and no organism
spends any budget: there is still no starvation, death or repair rule.

Native Release, clang-tidy analysis, ASan and format checks pass 19/19;
every existing golden and fingerprint is unchanged. Linux CI has not run.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-09-23 11:31:01 -04:00
co-authored by Claude Opus 5
parent 3f86dada0e
commit be71d4f441
13 changed files with 729 additions and 10 deletions
+161
View File
@@ -0,0 +1,161 @@
#include "evo/biology/metabolism.hpp"
#include <limits>
namespace evo::biology {
namespace {
constexpr std::uint64_t thousand = 1000;
// Membrane is the more carbon-rich compartment, so a thicker one changes what a
// body is made of and not only how much of it there is.
constexpr Composition structure_shares{500, 150, 50, 300};
constexpr Composition membrane_shares{700, 50, 100, 150};
constexpr std::uint32_t total_share(const Composition& shares) noexcept {
std::uint32_t sum = 0;
for (const auto share : shares) {
sum += share;
}
return sum;
}
static_assert(total_share(structure_shares) == thousand);
static_assert(total_share(membrane_shares) == thousand);
bool multiply(std::uint64_t& result, std::uint64_t left, std::uint64_t right) noexcept {
if (left != 0 && right > std::numeric_limits<std::uint64_t>::max() / left) {
return false;
}
result = left * right;
return true;
}
bool add(std::uint64_t& result, std::uint64_t left, std::uint64_t right) noexcept {
if (right > std::numeric_limits<std::uint64_t>::max() - left) {
return false;
}
result = left + right;
return true;
}
// Declared flooring, matching ADR-0031's derivation.
std::uint64_t scaled(std::uint64_t value, std::uint64_t numerator) noexcept {
return value / thousand * numerator + value % thousand * numerator / thousand;
}
// Splits a mass across the ledger's channels. The last channel takes whatever the
// first three left, so the parts always sum back to the whole.
accounting::Inventory compose(std::uint64_t mass, const Composition& shares) noexcept {
accounting::Inventory parts{};
std::uint64_t assigned = 0;
for (std::size_t index = 0; index + 1 < shares.size(); ++index) {
const auto part = scaled(mass, shares[index]);
parts.matter[index].value = part;
assigned += part;
}
parts.matter[shares.size() - 1].value = mass - assigned;
return parts;
}
bool combine(accounting::Inventory& total, const accounting::Inventory& amount) noexcept {
if (!add(total.energy.value, total.energy.value, amount.energy.value)) {
return false;
}
for (std::size_t index = 0; index < total.matter.size(); ++index) {
if (!add(total.matter[index].value, total.matter[index].value,
amount.matter[index].value)) {
return false;
}
}
return true;
}
std::uint64_t matter_total(const accounting::Inventory& inventory) noexcept {
std::uint64_t sum = 0;
for (const auto& mass : inventory.matter) {
sum += mass.value;
}
return sum;
}
// Scales an inventory's matter while keeping its composition. The last channel
// again takes the remainder, so the parts sum to the scaled whole exactly.
accounting::Inventory fraction_of(const accounting::Inventory& source,
std::uint64_t numerator) noexcept {
accounting::Inventory parts{};
const auto whole = scaled(matter_total(source), numerator);
std::uint64_t assigned = 0;
for (std::size_t index = 0; index + 1 < source.matter.size(); ++index) {
const auto part = scaled(source.matter[index].value, numerator);
parts.matter[index].value = part;
assigned += part;
}
parts.matter[source.matter.size() - 1].value = whole - assigned;
return parts;
}
} // namespace
std::expected<MetabolicRules, DevelopmentError>
metabolic_rules_for(std::uint32_t development_version) {
if (development_version != 1) {
return std::unexpected{DevelopmentError::unsupported_rules};
}
return MetabolicRules{structure_shares, membrane_shares, 20, 50'000};
}
std::expected<MetabolicBudget, DevelopmentError>
metabolic_budget(const Genome& genome, GrowthState growth, EnvironmentState environment) {
const auto phenotype = derive_phenotype(genome, growth, environment);
if (!phenotype) {
return std::unexpected{phenotype.error()};
}
const auto rules = metabolic_rules_for(genome.development_version());
if (!rules) {
return std::unexpected{rules.error()};
}
const auto shape = rules_for(genome.development_version());
if (!shape) {
return std::unexpected{shape.error()};
}
MetabolicBudget budget;
budget.phenotype = *phenotype;
budget.assimilation_per_thousand =
static_cast<std::uint16_t>(phenotype->conversion_per_thousand);
budget.body = compose(phenotype->structural_mass, rules->structure);
if (!combine(budget.body, compose(phenotype->membrane_mass, rules->membrane))) {
return std::unexpected{DevelopmentError::overflow};
}
budget.maintenance.value = phenotype->maintenance;
// Moving a larger body costs more, so a thicker membrane is paid for twice.
std::uint64_t haulage{};
if (!multiply(haulage, scaled(phenotype->total_mass, rules->movement_per_kilogram),
phenotype->movement_speed)) {
return std::unexpected{DevelopmentError::overflow};
}
budget.movement.value = haulage / thousand;
if (!add(budget.demand.value, budget.maintenance.value, budget.movement.value)) {
return std::unexpected{DevelopmentError::overflow};
}
// Intake funds structure, so it arrives in the proportions structure needs.
budget.intake_limit = compose(phenotype->ingestion_rate, rules->structure);
budget.intake_limit.energy.value =
scaled(phenotype->ingestion_rate, rules->intake_energy_per_kilogram);
const auto plan = plan_development(genome, growth, environment);
if (!plan) {
return std::unexpected{plan.error()};
}
const auto grown = derive_phenotype(genome, plan->after, environment);
if (!grown) {
return std::unexpected{grown.error()};
}
// Each compartment is composed separately: the two differ in what they cost.
budget.construction =
compose(grown->structural_mass - phenotype->structural_mass, rules->structure);
if (!combine(budget.construction,
compose(grown->membrane_mass - phenotype->membrane_mass, rules->membrane))) {
return std::unexpected{DevelopmentError::overflow};
}
budget.construction.energy.value = plan->energy_required;
if (genome.carries(Capability::reproduction)) {
const auto fraction = *genome.parameter(Capability::reproduction);
// A parent divides the body it has, so the investment keeps its composition.
budget.reproduction_investment = fraction_of(budget.body, fraction);
// Offspring tissue is priced at what the parent's own tissue costs.
budget.reproduction_investment.energy.value =
scaled(matter_total(budget.reproduction_investment), shape->construction_per_kilogram);
}
return budget;
}
} // namespace evo::biology