Decide affordability where composition is known

A construction transition could require more of an element than was
available, because the check compared net mass. A step growing a body
from 13 to 14 femtograms nets one femtogram but acquires a carbon and a
nitrogen while freeing one of other, and released other pays for neither.
Matching totals let one element silently fund another.

Net mass was never the right question, and the layer asking it could not
ask a better one: plan_development has no composition. A single scalar
pool could not carry the answer either, since it cannot say that carbon
is plentiful while nitrogen is not. The environment now states what is
available per ledger channel.

plan_development keeps a total-mass bound, which is sound because net
never exceeds gross, but it is a proposal rather than an authority.
metabolic_budget narrows it to the largest step whose own intake fits
channel by channel, and reports the growth that step actually funds. That
revises ADR-0031 on this point, recorded in both ADRs.

The tests place a single femtogram in each channel in turn and require
that no step needing a different element is funded, that nothing is
acquired beyond what is present, and that the mass-only plan would have
admitted the step; a carbon and a nitrogen together then fund it exactly.
Across six target sizes by seven growth fractions by four stock levels,
every reported intake fits the availability channel by channel and never
exceeds the proposed step. Confirmed to fail with the check removed.

The releasing case moved to a target of 20 femtograms, where the step
occurs at the full rule step rather than depending on a scarce supply
that the corrected plan no longer bounds the same way.

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:02:44 -04:00
co-authored by Claude Opus 5
parent e0fd4680f6
commit 36ee71cfe9
9 changed files with 226 additions and 55 deletions
+98 -16
View File
@@ -38,7 +38,23 @@ GenomeDraft capable() {
source.capabilities.push_back({Capability::reproduction, 300, 0});
return source;
}
constexpr EnvironmentState temperate{293'150, 1'000'000'000};
// Every channel stocked alike, so availability never accidentally hides a
// per-element shortfall except where a test sets one deliberately.
constexpr Inventory pooled(std::uint64_t value) {
return {{value}, {Femtograms{value}, Femtograms{value}, Femtograms{value}, Femtograms{value}}};
}
constexpr EnvironmentState temperate{293'150, pooled(1'000'000'000)};
bool fits_within(const Inventory& needed, const Inventory& available) {
if (needed.energy.value > available.energy.value) {
return false;
}
for (std::size_t index = 0; index < 4; ++index) {
if (needed.matter[index].value > available.matter[index].value) {
return false;
}
}
return true;
}
std::uint64_t matter_total(const Inventory& inventory) {
std::uint64_t sum = 0;
for (const auto& mass : inventory.matter) {
@@ -100,8 +116,9 @@ void construction_composes_each_compartment() {
CHECK(plan && plan->material_required == matter_total(budget->construction.intake) -
matter_total(budget->construction.release));
CHECK(plan->energy_required == budget->construction.intake.energy.value);
CHECK(budget->construction.after == plan->after);
// Unaffordable growth leaves nothing to move.
const EnvironmentState barren{293'150, 0};
const EnvironmentState barren{293'150, pooled(0)};
const auto starved = metabolic_budget(genome, {500, 0}, barren);
CHECK(starved && matter_total(starved->construction.intake) == 0);
CHECK(matter_total(starved->construction.release) == 0);
@@ -134,19 +151,22 @@ void construction_reproduces_the_derived_body() {
before->construction.release.matter[index].value ==
after->body.matter[index].value);
}
// A step can also free a femtogram as the declared shares rebalance: this
// body goes from 13 to 14 fg, and other falls from 6 to 5.
source.capabilities[2].parameter = 1000;
// A step can also free a femtogram as the declared shares rebalance. At a
// target of 20 fg a full step moves the body from 13 to 14 fg: carbon and
// nitrogen each gain one while other falls from 6 to 5.
source.capabilities[2].parameter = 20;
const auto small = build(source);
const EnvironmentState scarce{293'150, 1};
const auto crawling = metabolic_budget(small, {13, 0}, scarce);
CHECK(crawling);
const auto crawling = metabolic_budget(small, {650, 0}, temperate);
CHECK(crawling && crawling->phenotype.structural_mass == 13);
CHECK(crawling->body.matter[3].value == 6);
CHECK(crawling->construction.after == (GrowthState{700, 0}));
CHECK(crawling->construction.intake.matter[0].value == 1);
CHECK(crawling->construction.intake.matter[1].value == 1);
CHECK(crawling->construction.intake.matter[2].value == 0);
CHECK(crawling->construction.release.matter[3].value == 1);
const auto stepped = metabolic_budget(small, {14, 0}, scarce);
CHECK(stepped && stepped->body.matter[3].value == 5);
const auto stepped = metabolic_budget(small, {700, 0}, temperate);
CHECK(stepped && stepped->phenotype.structural_mass == 14);
CHECK(stepped->body.matter[3].value == 5);
for (std::size_t index = 0; index < 4; ++index) {
CHECK(crawling->body.matter[index].value +
crawling->construction.intake.matter[index].value -
@@ -155,6 +175,65 @@ void construction_reproduces_the_derived_body() {
}
}
void released_matter_never_funds_another_element() {
// The 13 to 14 fg step again: net growth is one femtogram, but it acquires a
// carbon and a nitrogen while freeing one of other. Released other is neither
// carbon nor nitrogen, so a single femtogram cannot fund it whatever channel
// that femtogram sits in.
auto source = draft();
source.capabilities[2].parameter = 20;
const auto genome = build(source);
for (std::size_t channel = 0; channel < 4; ++channel) {
Inventory single{{1'000'000}, {}};
single.matter[channel].value = 1; // One femtogram, and only one.
const EnvironmentState scarce{293'150, single};
// The mass-only plan is what would be fooled: it sees a net of one
// femtogram against a pool of one and admits the step.
const auto plan = plan_development(genome, {650, 0}, scarce);
CHECK(plan && plan->after == (GrowthState{700, 0}) && plan->material_required == 1);
const auto budget = metabolic_budget(genome, {650, 0}, scarce);
CHECK(budget);
CHECK(budget->construction.after != (GrowthState{700, 0})); // Not funded.
// Nothing is acquired at all: the steps that remain change no mass.
CHECK(matter_total(budget->construction.intake) == 0);
CHECK(matter_total(budget->construction.release) == 0);
CHECK(fits_within(budget->construction.intake, single));
const auto reached = metabolic_budget(genome, budget->construction.after, scarce);
CHECK(reached && reached->phenotype.structural_mass == 13);
}
// A carbon and a nitrogen together are exactly what the step needs.
Inventory exact{{1'000'000}, {}};
exact.matter[0].value = 1;
exact.matter[1].value = 1;
const auto precise = metabolic_budget(genome, {650, 0}, {293'150, exact});
CHECK(precise && precise->construction.after == (GrowthState{700, 0}));
CHECK(precise->construction.intake.matter[0].value == 1);
CHECK(precise->construction.intake.matter[1].value == 1);
CHECK(precise->construction.release.matter[3].value == 1);
}
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}) {
source.capabilities[2].parameter = target;
const auto genome = build(source);
for (const std::uint32_t realized : {0U, 1U, 13U, 333U, 500U, 950U, 1000U}) {
for (const std::uint64_t stock : {0ULL, 1ULL, 7ULL, 1'000'000ULL}) {
const EnvironmentState environment{293'150, pooled(stock)};
const auto budget = metabolic_budget(genome, {realized, 0}, environment);
CHECK(budget);
// Whatever a step reports, every element of it must be there.
CHECK(fits_within(budget->construction.intake, environment.available));
// A funded step never exceeds what the mass-only plan proposed.
const auto plan = plan_development(genome, {realized, 0}, environment);
CHECK(plan && budget->construction.after.realized_per_thousand <=
plan->after.realized_per_thousand);
CHECK(budget->construction.after.damage_per_thousand == 0);
}
}
}
}
void every_transition_conserves_each_channel() {
auto source = capable();
for (const std::uint32_t target : {1U, 999U, 1001U, 9'999U, 123'457U, 8'000'000U}) {
@@ -162,11 +241,12 @@ void every_transition_conserves_each_channel() {
const auto genome = build(source);
for (const std::uint32_t realized : {0U, 1U, 13U, 333U, 500U, 950U, 1000U}) {
for (const std::uint64_t material : {0ULL, 1ULL, 7ULL, 1'000'000'000ULL}) {
const EnvironmentState environment{293'150, material};
const EnvironmentState environment{293'150, pooled(material)};
const auto budget = metabolic_budget(genome, {realized, 0}, environment);
const auto plan = plan_development(genome, {realized, 0}, environment);
CHECK(budget && plan);
const auto reached = metabolic_budget(genome, plan->after, environment);
const auto reached =
metabolic_budget(genome, budget->construction.after, environment);
CHECK(reached);
// Spending the budget must reproduce the derived body exactly, on
// every channel, not merely in total.
@@ -180,7 +260,7 @@ void every_transition_conserves_each_channel() {
CHECK(matter_total(budget->construction.intake) >=
matter_total(budget->construction.release));
CHECK(matter_total(budget->construction.intake) -
matter_total(budget->construction.release) ==
matter_total(budget->construction.release) <=
plan->material_required);
}
}
@@ -199,13 +279,13 @@ void repeated_growth_conserves_each_channel() {
CHECK(opening);
for (int step = 0; step < 40 && growth.realized_per_thousand < 1000; ++step) {
const auto budget = metabolic_budget(genome, growth, temperate);
const auto plan = plan_development(genome, growth, temperate);
CHECK(budget && plan);
CHECK(budget &&
budget->construction.after.realized_per_thousand > growth.realized_per_thousand);
for (std::size_t index = 0; index < 4; ++index) {
acquired.matter[index].value += budget->construction.intake.matter[index].value;
freed.matter[index].value += budget->construction.release.matter[index].value;
}
growth = plan->after;
growth = budget->construction.after;
}
CHECK(growth.realized_per_thousand == 1000);
const auto finished = metabolic_budget(genome, growth, temperate);
@@ -374,6 +454,8 @@ int main() try {
hand_calculated_budget();
construction_composes_each_compartment();
construction_reproduces_the_derived_body();
released_matter_never_funds_another_element();
every_transition_is_affordable_channel_by_channel();
every_transition_conserves_each_channel();
repeated_growth_conserves_each_channel();
thicker_membrane_costs_matter_and_maintenance();