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
+11
View File
@@ -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
+5 -1
View File
@@ -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<std::uint64_t>::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;
}
+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();
+18
View File
@@ -1,6 +1,7 @@
#include "evo/processes/genome_strategy.hpp"
#include <iostream>
#include <limits>
#include <source_location>
#include <stdexcept>
#include <string>
@@ -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<std::uint64_t>::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();