Files
aevolution/tests/budding_tests.cpp
T
dingoandClaude Opus 5.5 d98d71e7c0
Foundation / checks (ecs) (push) Successful in 2m20s
Foundation / checks (deploy) (push) Successful in 4m4s
Foundation / checks (tsan) (push) Successful in 9m59s
Foundation / checks (client) (push) Successful in 19m19s
Foundation / checks (network) (push) Successful in 19m24s
Foundation / postgres-tools (push) Canceled after 0s
Foundation / server-image (push) Canceled after 0s
Foundation / image (push) Canceled after 0s
Foundation / checks (postgres) (push) Canceled after 32m13s
Foundation / checks (checks) (push) Canceled after 32m19s
Show what a bud costs (ADR-0129 amendment)
The v10 cellular perception carries the bud cost after the diagnostics block: the
energy (construction plus the bud's provision) and matter per channel one bud takes
from a fully grown cell's reserve. biology::bud_cost reads it from fund_bud itself,
so the shown cost is exactly the charge; the server caches it per genome. The
client shows it and names the channels the reserve is short on. v10 clients must
be rebuilt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 19:11:28 -04:00

160 lines
7.0 KiB
C++

// M11.5a (ADR-0125): the funded bud proposal.
#include "evo/biology/budding.hpp"
#include "evo/biology/founder.hpp"
#include <iostream>
#include <source_location>
#include <stdexcept>
#include <string>
namespace {
using namespace evo::biology;
using namespace evo::accounting;
void check(bool condition, std::source_location location = std::source_location::current()) {
if (!condition)
throw std::runtime_error("budding line " + std::to_string(location.line()));
}
#define CHECK(expression) check(static_cast<bool>(expression))
constexpr std::uint32_t temperate = 293'150;
Genome founder() {
const auto genome = primitive_founder(5, 5);
CHECK(genome);
return *genome;
}
Inventory plenty() {
constexpr std::uint64_t lots = 1'000'000'000'000;
return {{lots}, {Femtograms{lots}, Femtograms{lots}, Femtograms{lots}, Femtograms{lots}}};
}
// The parent's reserve before equals its reserve after plus the bud's body and reserve,
// plus heat in energy: nothing is created or lost on any channel.
bool conserves(const Inventory& before, const BudTransition& bud) {
for (std::size_t c = 0; c < before.matter.size(); ++c)
if (before.matter[c].value != bud.parent_reserve.matter[c].value +
bud.body.matter[c].value + bud.reserve.matter[c].value)
return false;
return bud.body.energy.value == 0 && before.energy.value == bud.parent_reserve.energy.value +
bud.reserve.energy.value +
bud.heat.value;
}
void a_grown_parent_funds_the_largest_bud_its_investment_builds() {
const auto genome = founder();
const auto reserve = plenty();
const auto bud = fund_bud(genome, {1000, 0}, reserve, temperate);
CHECK(bud);
CHECK(conserves(reserve, *bud));
const auto parent = metabolic_budget(genome, {1000, 0}, {temperate, {}});
CHECK(parent);
// The founder invests half its body, so the bud is a partly realized cell.
CHECK(bud->growth.realized_per_thousand > 0 && bud->growth.realized_per_thousand < 1000 &&
bud->growth.damage_per_thousand == 0);
CHECK(bud->heat == parent->reproduction_investment.energy);
const auto developed = metabolic_budget(genome, bud->growth, {temperate, {}});
CHECK(developed);
CHECK(bud->body.matter == developed->body.matter);
// One more step of growth would not fit the investment.
if (bud->growth.realized_per_thousand < 1000) {
const auto larger =
metabolic_budget(genome, {bud->growth.realized_per_thousand + 1, 0}, {temperate, {}});
CHECK(larger);
bool fits = true;
for (std::size_t c = 0; c < larger->body.matter.size(); ++c)
fits = fits &&
larger->body.matter[c].value <= parent->reproduction_investment.matter[c].value;
CHECK(!fits);
}
// The provision is 30 seconds of the bud's own full demand.
CHECK(bud->reserve.energy.value == developed->demand.value * bud_provision_seconds * 1'000'000 /
developed->rate_microseconds);
CHECK(fund_bud(genome, {1000, 0}, reserve, temperate) == bud); // Deterministic.
}
void every_short_channel_refuses_and_spends_nothing() {
const auto genome = founder();
const auto exact = fund_bud(genome, {1000, 0}, plenty(), temperate);
CHECK(exact);
// The exact cost: what the parent's reserve lost.
Inventory cost;
for (std::size_t c = 0; c < cost.matter.size(); ++c)
cost.matter[c].value = plenty().matter[c].value - exact->parent_reserve.matter[c].value;
cost.energy.value = plenty().energy.value - exact->parent_reserve.energy.value;
const auto paid = fund_bud(genome, {1000, 0}, cost, temperate);
CHECK(paid && paid->parent_reserve == Inventory{} && conserves(cost, *paid));
for (std::size_t c = 0; c <= cost.matter.size(); ++c) {
auto short_one = cost;
if (c == cost.matter.size()) {
if (short_one.energy.value == 0)
continue;
--short_one.energy.value;
} else {
if (short_one.matter[c].value == 0)
continue;
--short_one.matter[c].value;
// Abundant energy never pays for missing matter.
short_one.energy.value += 1'000'000'000;
}
CHECK(fund_bud(genome, {1000, 0}, short_one, temperate).error() ==
BuddingError::insufficient_funding);
}
}
void only_a_grown_undamaged_reproducing_parent_buds() {
const auto genome = founder();
CHECK(fund_bud(genome, {999, 0}, plenty(), temperate).error() == BuddingError::not_grown);
CHECK(fund_bud(genome, {1000, 1}, plenty(), temperate).error() == BuddingError::not_grown);
const auto barren = Genome::create({9,
1,
2,
{},
{{Capability::energy_conversion, 400, 0},
{Capability::membrane, 1, 0},
{Capability::target_size, 8'000'000, 0}}});
CHECK(barren);
CHECK(fund_bud(*barren, {1000, 0}, plenty(), temperate).error() == BuddingError::no_capability);
}
// ADR-0129 amendment: the shown bud cost is exactly the charge. A reserve equal to it buds;
// one unit short on any channel refuses.
void the_shown_bud_cost_is_exactly_what_funding_takes() {
const auto genome = founder();
const auto shown = bud_cost(genome, temperate);
if (!shown)
throw std::runtime_error("budding: founder has no bud cost");
const Inventory cost = *shown;
CHECK(cost.energy.value != 0);
const auto exact = fund_bud(genome, {1000, 0}, cost, temperate);
CHECK(exact && exact->parent_reserve == Inventory{});
for (std::size_t c = 0; c <= cost.matter.size(); ++c) {
auto short_one = cost;
auto& unit = c == 0 ? short_one.energy.value : short_one.matter[c - 1].value;
if (unit == 0)
continue;
--unit;
CHECK(fund_bud(genome, {1000, 0}, short_one, temperate).error() ==
BuddingError::insufficient_funding);
}
const auto barren = Genome::create({9,
1,
2,
{},
{{Capability::energy_conversion, 400, 0},
{Capability::membrane, 1, 0},
{Capability::target_size, 8'000'000, 0}}});
CHECK(barren && !bud_cost(*barren, temperate));
}
} // namespace
int main() {
try {
a_grown_parent_funds_the_largest_bud_its_investment_builds();
every_short_channel_refuses_and_spends_nothing();
only_a_grown_undamaged_reproducing_parent_buds();
the_shown_bud_cost_is_exactly_what_funding_takes();
} catch (const std::exception& error) {
std::cerr << error.what() << '\n';
return 1;
}
return 0;
}