diff --git a/docs/engineering/VALIDATION.md b/docs/engineering/VALIDATION.md index cfc86aa..87113b0 100644 --- a/docs/engineering/VALIDATION.md +++ b/docs/engineering/VALIDATION.md @@ -553,6 +553,17 @@ checked to fit the availability channel by channel and never to exceed the step the plan proposed. These tests were confirmed to fail with the per-channel check removed. +A third review found the availability bound itself could wrap: summing four +unrestricted u64 channels with 2^62 in each reaches exactly 2^64, so the total +read as zero and both planning and budgeting refused growth that a billion +femtograms per channel funded. The sum now saturates, which is also the right +answer since no step can need more than u64. Regressions in both suites use that +exact 2^62 case and a single saturated channel, and require the same step a +billion per channel funds; both were confirmed to fail without saturation. The +equivalent helper in `metabolism.cpp` is applied only to inventories derived from +bounded phenotype masses, never to caller input, so it is left unsaturated rather +than given a branch no test could reach. + 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 diff --git a/src/biology/development.cpp b/src/biology/development.cpp index 4fec129..b2e069b 100644 --- a/src/biology/development.cpp +++ b/src/biology/development.cpp @@ -39,10 +39,14 @@ std::uint64_t scaled(std::uint64_t value, std::uint64_t numerator) noexcept { // Declared flooring; the remainder is discarded rather than carried. return value / thousand * numerator + value % thousand * numerator / thousand; } +// Saturating on purpose. Availability is caller-supplied and unrestricted, and a +// wrapped total would read as no material at all, refusing growth that is plainly +// funded. Saturation is also the right answer: no step can need more than u64. std::uint64_t matter_total(const accounting::Inventory& inventory) noexcept { + constexpr auto maximum = std::numeric_limits::max(); std::uint64_t sum = 0; for (const auto& mass : inventory.matter) { - sum += mass.value; + sum = mass.value > maximum - sum ? maximum : sum + mass.value; } return sum; } diff --git a/tests/development_tests.cpp b/tests/development_tests.cpp index 16a3a66..12a2e36 100644 --- a/tests/development_tests.cpp +++ b/tests/development_tests.cpp @@ -1,6 +1,7 @@ #include "evo/biology/development.hpp" #include +#include #include #include #include @@ -162,6 +163,30 @@ void rule_versions_and_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::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::max()}, + {evo::accounting::Femtograms{std::numeric_limits::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 @@ -249,6 +274,7 @@ int main() try { capability_prerequisites(); 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(); diff --git a/tests/metabolism_tests.cpp b/tests/metabolism_tests.cpp index 8badf07..bb0c025 100644 --- a/tests/metabolism_tests.cpp +++ b/tests/metabolism_tests.cpp @@ -1,6 +1,7 @@ #include "evo/processes/genome_strategy.hpp" #include +#include #include #include #include @@ -212,6 +213,22 @@ void released_matter_never_funds_another_element() { 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::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}) { @@ -455,6 +472,7 @@ int main() try { 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();