Make construction reproduce the body it claims to build

Construction composed the mass difference instead of differencing the two
composed bodies. Those are not the same: composing the difference throws
away the rounding the existing body already carries, so the budget summed
to the right total while funding individual channels short or over. At
target mass 1001 growing from 10 to 60 it funded C25 N7 P2 other16,
leaving nitrogen and phosphorus one short each and other two over, so
spending it 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.

That difference can be negative on a channel, because the remainder rule
is not monotone: a body growing from 13 to 14 femtograms sees other fall
from 6 to 5. Construction is therefore a transition with an intake and a
release rather than a single requirement, and the release is a rounding
artifact bounded by a few femtograms, not excretion.

The invariant held + intake - release == reached is asserted per channel
for the reported 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 accumulated intake and release are
checked against the whole history. The net of every transition is also
asserted to equal what a plan charges material for. These tests were
confirmed to fail against the previous implementation.

Native Release, clang-tidy analysis, ASan and format checks pass 19/19.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
dingo
2026-09-23 12:26:40 -04:00
co-authored by Claude Opus 5
parent be71d4f441
commit e0fd4680f6
5 changed files with 197 additions and 25 deletions
+18 -6
View File
@@ -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.