Stop the availability bound wrapping to zero

The helper summing available material added four unrestricted u64
channels without a check. With 2^62 in each, the sum reaches exactly 2^64
and wraps to zero, so the bound read as no material at all and both
planning and budgeting refused growth that a billion femtograms per
channel funds.

The sum now saturates. That is also the right answer rather than merely a
safe one: no step can need more than u64 femtograms, so a saturated bound
is correctly permissive.

Regressions in both suites use the exact 2^62 case and a single saturated
channel, and require the step that an ordinary supply 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 alone rather than given a branch no test could reach. That is
recorded in the validation report.

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 13:19:29 -04:00
co-authored by Claude Opus 5
parent 36ee71cfe9
commit fd6faeb7f8
4 changed files with 60 additions and 1 deletions
+26
View File
@@ -1,6 +1,7 @@
#include "evo/biology/development.hpp"
#include <iostream>
#include <limits>
#include <source_location>
#include <stdexcept>
#include <string>
@@ -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<std::uint64_t>::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<std::uint64_t>::max()},
{evo::accounting::Femtograms{std::numeric_limits<std::uint64_t>::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();