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
+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;
}