diff --git a/docs/adr/0032-metabolic-budgets-from-genome.md b/docs/adr/0032-metabolic-budgets-from-genome.md index d40d694..1f1a27b 100644 --- a/docs/adr/0032-metabolic-budgets-from-genome.md +++ b/docs/adr/0032-metabolic-budgets-from-genome.md @@ -42,6 +42,17 @@ of and not only how much. A split assigns the first three channels by share and gives the remainder to the last, so mass is conserved exactly and a body can be spent through the ledger without a rounding leak. +Construction is the difference between the two composed bodies, not the +composition of the mass difference. Those are not the same: composing the +difference discards the rounding the existing body already carries, so a budget +built that way sums to the right total while funding individual channels short or +over, and spending it could not produce the body the same rules derive. Because +that remainder rule is not monotone per channel, a step can also free a femtogram +as the shares rebalance, so construction is a transition with both an intake and a +release rather than a single requirement. The release is a rounding artifact +bounded by a few femtograms, not excretion, and the invariant is exact: the body +held, plus intake, minus release, is the body reached, on every channel. + Reproduction investment is the body scaled by the genome's reproduction fraction, with its energy priced at the same construction rate as the organism's own tissue: building offspring tissue costs what building your own does. A genome without the diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index d8b5cc8..5f17f34 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -519,6 +519,28 @@ drives a real `FeedingCommunity` tick, the ledger totals are identical before an after, and a bite one femtogram past the derived cap is refused by the existing M2.3 contract. The opening inventory is checked to be exactly the caller's. +Review of `be71d4f` found one defect (2026-09-23): construction composed the mass +difference rather than differencing the two composed bodies, which discards the +rounding the existing body already carries. At target mass 1001 growing from 10 to +60, it funded C25 N7 P2 other16: the right total, but nitrogen and phosphorus one +short each and other two over, so spending the budget could not produce the body +the same rules derive. Construction is now the per-channel difference between the +composed bodies, which funds C25 N8 P3 other14 for that case. + +Because the remainder rule is not monotone per channel, that difference can be +negative on a channel, so construction became a transition with an intake and a +release rather than a single requirement: a body growing from 13 to 14 femtograms +acquires one carbon and one nitrogen while freeing one from other. The release is +a rounding artifact bounded by a few femtograms, not excretion. + +The invariant `held + intake - release == reached` is now asserted per channel for +the reviewer's exact case, for the releasing case, across six target sizes by +seven growth fractions by four material budgets, and across a forty-step walk from +nothing to full growth where the accumulated intake and release are checked +against the whole history. The net of every transition is also asserted to equal +what `plan_development` charges material for. These tests were confirmed to fail +against the previous implementation. + Coverage limitation: as in M3.2, the overflow guards are unreachable through the public API at schema 1 ranges and are recorded rather than covered by a test that could not fail. The small integer helpers are duplicated between `development.cpp` diff --git a/include/evo/biology/metabolism.hpp b/include/evo/biology/metabolism.hpp index 9b736bd..43ee1e8 100644 --- a/include/evo/biology/metabolism.hpp +++ b/include/evo/biology/metabolism.hpp @@ -17,17 +17,27 @@ struct MetabolicRules { // Picojoules of intake capacity per 1000 femtograms of it. std::uint64_t intake_energy_per_kilogram; }; +// The exact per-channel move from the body an organism has to the one a growth +// step produces. Composing the mass difference instead would discard the rounding +// the existing body already carries, so spending the budget could not reproduce +// the derived body on every channel. Release is a rounding artifact of the +// declared composition, bounded by a few femtograms; it is not excretion. +struct ConstructionTransition { + accounting::Inventory intake; // energy and matter that must be acquired + accounting::Inventory release; // matter freed as the composition rebalances + auto operator<=>(const ConstructionTransition&) const = default; +}; // Every budget a phenotype implies. Each is derived from a different phenotype // quantity, so one genome parameter moves some and not others: none of this is a // multiplier applied to a total. struct MetabolicBudget { Phenotype phenotype; - accounting::Inventory body; // matter only; a body is not energy - accounting::Picojoules maintenance; // per tick - accounting::Picojoules movement; // per tick at full speed, a capacity - accounting::Picojoules demand; // maintenance plus movement - accounting::Inventory construction; // what the next growth step would take - accounting::Inventory intake_limit; // per tick + accounting::Inventory body; // matter only; a body is not energy + accounting::Picojoules maintenance; // per tick + accounting::Picojoules movement; // per tick at full speed, a capacity + accounting::Picojoules demand; // maintenance plus movement + ConstructionTransition construction; // what the next growth step would move + accounting::Inventory intake_limit; // per tick // Zero without the reproduction capability, rather than a default. accounting::Inventory reproduction_investment; std::uint16_t assimilation_per_thousand{}; diff --git a/src/biology/metabolism.cpp b/src/biology/metabolism.cpp index ae2b7dc..4b03d04 100644 --- a/src/biology/metabolism.cpp +++ b/src/biology/metabolism.cpp @@ -140,14 +140,26 @@ metabolic_budget(const Genome& genome, GrowthState growth, EnvironmentState envi 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))) { + // Compose the grown body and difference it against the current one. Composing + // the mass difference instead would discard the rounding this body already + // carries, and the budget could not then reproduce the derived body channel by + // channel. Each compartment is still composed separately: the two differ in + // what they cost. + auto reached = compose(grown->structural_mass, rules->structure); + if (!combine(reached, compose(grown->membrane_mass, rules->membrane))) { return std::unexpected{DevelopmentError::overflow}; } - budget.construction.energy.value = plan->energy_required; + for (std::size_t index = 0; index < reached.matter.size(); ++index) { + const auto held = budget.body.matter[index].value; + const auto wanted = reached.matter[index].value; + if (wanted >= held) { + budget.construction.intake.matter[index].value = wanted - held; + } else { + budget.construction.release.matter[index].value = held - wanted; + } + } + // Energy is priced on the net mass change, matching what the plan reports. + budget.construction.intake.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. diff --git a/tests/metabolism_tests.cpp b/tests/metabolism_tests.cpp index 6de1711..f8749d3 100644 --- a/tests/metabolism_tests.cpp +++ b/tests/metabolism_tests.cpp @@ -77,7 +77,9 @@ void hand_calculated_budget() { 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) == 0 && budget->construction.energy.value == 0); + 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() { @@ -86,21 +88,133 @@ void construction_composes_each_compartment() { 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.matter[0].value == 200'000 + 1'562'400); - CHECK(budget->construction.matter[1].value == 60'000 + 111'600); - CHECK(budget->construction.matter[2].value == 20'000 + 223'200); - CHECK(budget->construction.matter[3].value == 120'000 + 334'800); - CHECK(matter_total(budget->construction) == 2'632'000); - CHECK(budget->construction.energy.value == 105'280); - // What a plan reports as material is exactly what the composition sums to. + 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)); - CHECK(plan->energy_required == budget->construction.energy.value); - // Unaffordable growth leaves nothing to compose. + CHECK(plan && plan->material_required == matter_total(budget->construction.intake) - + matter_total(budget->construction.release)); + CHECK(plan->energy_required == budget->construction.intake.energy.value); + // Unaffordable growth leaves nothing to move. const EnvironmentState barren{293'150, 0}; const auto starved = metabolic_budget(genome, {500, 0}, barren); - CHECK(starved && matter_total(starved->construction) == 0); - CHECK(starved->construction.energy.value == 0); + 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: this + // body goes from 13 to 14 fg, and other falls from 6 to 5. + source.capabilities[2].parameter = 1000; + const auto small = build(source); + const EnvironmentState scarce{293'150, 1}; + const auto crawling = metabolic_budget(small, {13, 0}, scarce); + CHECK(crawling); + CHECK(crawling->body.matter[3].value == 6); + CHECK(crawling->construction.intake.matter[0].value == 1); + CHECK(crawling->construction.intake.matter[1].value == 1); + CHECK(crawling->construction.release.matter[3].value == 1); + const auto stepped = metabolic_budget(small, {14, 0}, scarce); + CHECK(stepped && 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 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, 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, plan->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); + const auto plan = plan_development(genome, growth, temperate); + CHECK(budget && plan); + 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 = plan->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() { @@ -259,6 +373,9 @@ void a_genome_drives_a_feeding_tick() { int main() try { hand_calculated_budget(); construction_composes_each_compartment(); + construction_reproduces_the_derived_body(); + 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();