Files
aevolution/tests/budding_tests.cpp
dingoandClaude Opus 5.5 9f117c9545
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (tsan) (push) Successful in 5s
Foundation / checks (deploy) (push) Successful in 4m22s
Foundation / postgres-tools (push) Successful in 22s
Foundation / server-image (push) Successful in 4m21s
Foundation / image (push) Successful in 5m6s
feat(lifecycle): rules 13 mutation splits, stay-or-become and litter
Phase 4 of the cellular revamp build plan (ADR-0179), server half.

- A chosen mutation now splits the cell (Cellular_reproduction_plan.md section
  4): split_cell divides body and reserve per channel by the tuned share, each
  cell develops the largest body its own genome builds from its part, and the
  rest joins its reserve, conserving every channel. Both plans carry the
  mutation; only the offspring expresses it. A split is refused, spending
  nothing and keeping the offer, below the tuned growth, without room beside
  the cell or at the region's organism cap.
- Stay or become: a choice (trace mutation byte 34) or a bud (step intent bit 2
  of byte 13) can hand control to the offspring; the parent becomes wildlife.
  Ownership now follows the account under rules 13, so validation and plan
  history accept a founder that became wildlife (ADR-0163 succession).
- Buds under rules 13: litter replaces the 25% twin (whole hundreds plus a
  chance, up to the tuned maximum, sharing one investment), the tuned bud fee is
  charged once and reclaimed, and births stop at the organism cap.
- New results split_applied and split_refused; owners below rules 13 refuse a
  become.

The revamp tuning file gains the reproduction keys. Tests cover the split's
conservation and refusals, stay and become splits with exact restore, a refused
second split, the cap, become validation and the litter count. Refreshes the DEV
session window.

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

236 lines
11 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(...) check(static_cast<bool>(__VA_ARGS__))
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
// Rules 13 (ADR-0179): a split divides body and reserve, each cell builds the largest body its
// part covers under its own genome, and every channel is conserved.
Genome founder6(std::uint64_t id = 9) {
const auto genome = primitive_founder(id, id, primitive_founder_v6);
CHECK(genome);
return *genome;
}
bool split_conserves(const Inventory& body, const Inventory& reserve, const SplitTransition& s) {
for (std::size_t c = 0; c < body.matter.size(); ++c)
if (body.matter[c].value + reserve.matter[c].value !=
s.parent_body.matter[c].value + s.parent_reserve.matter[c].value +
s.child_body.matter[c].value + s.child_reserve.matter[c].value)
return false;
return reserve.energy.value == s.parent_reserve.energy.value + s.child_reserve.energy.value &&
s.parent_body.energy.value == 0 && s.child_body.energy.value == 0;
}
void a_split_halves_and_conserves() {
const auto parent = founder6();
const auto grown = metabolic_budget(parent, {1000, 0}, {temperate, {}});
CHECK(grown);
const Inventory reserve{{2'000'001},
{Femtograms{301}, Femtograms{7}, Femtograms{0}, Femtograms{1'000}}};
const auto split =
split_cell(parent, parent, {1000, 0}, grown->body, reserve, temperate, 500, 1000);
CHECK(split && split_conserves(grown->body, reserve, *split));
// Half the body builds a cell under half full size (membrane follows surface, not volume);
// the two halves are equal but for rounding.
CHECK(split->parent_growth.realized_per_thousand >= 300 &&
split->parent_growth.realized_per_thousand < 500 &&
split->child_growth == split->parent_growth);
CHECK(split->child_reserve.energy.value == 1'000'000 &&
split->parent_reserve.energy.value == 1'000'001);
// A different child plan develops its own body from its half.
GenomeDraft larger;
larger.schema_version = genome_schema_v7;
larger.id = 10;
larger.origin_event = 10;
larger.development_version = 7;
larger.parents = {parent.id()};
for (const auto& record : parent.capabilities())
larger.capabilities.push_back(record);
for (auto& record : larger.capabilities)
if (record.kind == Capability::target_size)
record.parameter = 8'400'000;
larger.movement_arrangement = parent.movement_arrangement();
larger.cellular_traits = parent.cellular_traits();
const auto child = Genome::create(larger);
CHECK(child);
const auto mutated =
split_cell(parent, *child, {1000, 0}, grown->body, reserve, temperate, 500, 1000);
CHECK(mutated && split_conserves(grown->body, reserve, *mutated) &&
mutated->child_growth.realized_per_thousand <
mutated->parent_growth.realized_per_thousand);
}
void a_split_refuses_without_changing_anything() {
const auto parent = founder6();
const auto grown = metabolic_budget(parent, {1000, 0}, {temperate, {}});
CHECK(grown);
CHECK(split_cell(parent, parent, {999, 0}, grown->body, {}, temperate, 500, 1000).error() ==
BuddingError::not_grown);
CHECK(split_cell(parent, parent, {1000, 1}, grown->body, {}, temperate, 500, 1000).error() ==
BuddingError::not_grown);
CHECK(split_cell(parent, parent, {1000, 0}, grown->body, {}, temperate, 0, 1000).error() ==
BuddingError::no_capability);
// A sliver of the body builds no cell.
CHECK(split_cell(parent, parent, {1000, 0}, grown->body, {}, temperate, 1, 1000).error() ==
BuddingError::insufficient_funding);
// A lower minimum lets a half-grown cell split.
const auto half = metabolic_budget(parent, {600, 0}, {temperate, {}});
CHECK(half && split_cell(parent, parent, {600, 0}, half->body, {}, temperate, 500, 500));
CHECK(split_cell(parent, founder(), {1000, 0}, grown->body, {}, temperate, 500, 1000).error() ==
BuddingError::incompatible_genome);
}
int main() {
try {
a_split_halves_and_conserves();
a_split_refuses_without_changing_anything();
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;
}