Files
aevolution/tests/cellular_revamp_step_tests.cpp
T
dingoandClaude Opus 5.5 7e5775b6f7
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 4s
Foundation / checks (network) (push) Successful in 4s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / checks (deploy) (push) Successful in 4m34s
Foundation / postgres-tools (push) Successful in 22s
Foundation / server-image (push) Successful in 4m16s
Foundation / image (push) Successful in 4m47s
feat(lifecycle): rules 13 behaviour-weighted mutation offers in the owner
Phase 3 of the cellular revamp build plan (ADR-0179), server half.

- Every owned, active rules-13 life gathers its activity each tick from every
  phase (thrust and drift, brake presses, feeding and the collection limits,
  matter burned by channel, energy by use, contact with larger cells, engulf
  outcomes, growth blocked by a channel, buds made, refused or lost young, the
  reserve against the body) and advances its 98 triggers and 49 affinity scores.
- Active play earns an offer every interval: eligible catalog mutations of the
  life's plan, weighted by affinity, drawn with the floored and capped chances
  and a seeded systematic draw (a retry draws the same offer). Wildlife,
  newborns and frozen cells earn nothing; a plan without schema-7 traits has no
  catalog.
- A choice must name the life's own pending offer. It edits the offspring plan
  (phase 4 turns this into the split), pays the mutation's tuned cost from the
  reserve (matter may stand in for its energy, excluded channels excepted) and
  reclaims it as seeded random bubbles, merging into the farthest deposit at the
  deposit limit. Unaffordable choices keep the offer; declines clear it.
- Offspring inherit affinity scores and lineage progress. Growth speed scales
  the growth step with a carried fraction.
- Format 11 persists the offer tunables and each life's mutation state; the
  revamp tuning file gains the offer keys and optional per-mutation overrides
  (mutation.<key>.<field>), defaulting to the catalog with zero costs.

Tests cover per-mutation overrides and refusals, the collection-limit flags,
offers from active play, founder-5 plans never offered, exact restore of a
pending offer, a choice editing only the plan and reclaiming its cost with
closure, unaffordable choices, declines, foreign choices, and thrust moving
affinity. Refreshes the DEV session window.

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

274 lines
12 KiB
C++

#include "evo/biology/cellular_region_step.hpp"
#include <array>
#include <cstdio>
#include <memory>
#include <source_location>
#include <stdexcept>
#include <string>
#include <utility>
// Rules 13 region step (ADR-0179; feeding and energy, predation and defense plans): per-channel
// collection with a shared limit, the membrane's collection cost, the kept share of bubble
// energy, matter standing in for energy, engulf eligibility with reserve mass and capacity, and
// the kill remainder split between bubbles and the spent pool. Everything conserves.
namespace {
using namespace evo::biology;
using evo::accounting::Inventory;
void check(bool value, std::source_location at = std::source_location::current()) {
if (!value)
throw std::runtime_error("cellular revamp step line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
constexpr CellularArea area{3'000, 2'000};
constexpr CellularRegionRules plain{true, true, CellularRevampStepRules{}};
Inventory inv(std::uint64_t e, std::uint64_t c, std::uint64_t n, std::uint64_t p, std::uint64_t o) {
return {{e}, {{{c}, {n}, {p}, {o}}}};
}
void add(Inventory& to, const Inventory& from) {
to.energy.value += from.energy.value;
for (std::size_t i = 0; i < to.matter.size(); ++i)
to.matter[i].value += from.matter[i].value;
}
// One bubble at (100, 100), or none.
std::unique_ptr<CellularHabitat> liquid_map(const Inventory& stock, bool bubble = true) {
CellularHabitatConfig config{7, 9, 11, area.width_um, area.height_um, {}};
config.format = cellular_habitat_liquid_format;
config.next_deposit = 2;
if (bubble) {
CellularPatch mote{
{100, 100}, 51, stock, 1, cellular_mote_radius_um, CellularDepositKind::mote};
mote.merged = 1;
config.patches.push_back(mote);
}
auto made = CellularHabitat::create(std::move(config));
CHECK(made);
return std::move(*made);
}
// Intake limits of 1,000 per second on every channel: 50 per 50 ms tick.
MetabolicBudget budget(std::uint64_t mass, std::uint64_t maintenance = 0) {
MetabolicBudget value{};
value.rate_microseconds = 1'000'000;
value.intake_limit = inv(1'000, 1'000, 1'000, 1'000, 1'000);
value.phenotype.total_mass = mass;
value.maintenance.value = maintenance;
return value;
}
CellularRevampLifeInput founder_input() {
CellularRevampLifeInput input{};
input.burn = cellular_burn({10, 10, 10, 10}, {100, 100, 100, 100}, 0);
return input;
}
// A still cell; no thrust, no current.
CellularLifeStepInput cell(std::uint64_t id, CellularPosition at, std::uint64_t mass,
const Inventory& reserve, bool engulf = false,
CellularRevampLifeInput revamp = founder_input(),
std::uint64_t maintenance = 0) {
CellularLifeStepInput life{};
life.life_id = id;
life.position = at;
life.extent_um = 20;
life.intent.engulf = engulf;
life.body = inv(0, 10, 10, 10, 10);
life.reserve = reserve;
life.budget = budget(mass, maintenance);
life.membrane_um = 1;
life.source = 51;
life.liquid = CellularLiquidInput{};
life.revamp = revamp;
return life;
}
// Everything the region holds after a candidate: lives (body, reserve, heat), deposits, corpses,
// heat released and the spent pool.
Inventory total(const CellularRegionStepCandidate& c, const CellularHabitat& map,
std::span<const CellularLifeStepInput> lives) {
Inventory sum{};
for (std::size_t i = 0; i < map.patches().size(); ++i) {
const auto update = std::ranges::find(c.patches, map.patches()[i].address,
&CellularPatchStockUpdate::address);
add(sum, update == c.patches.end() ? map.patches()[i].stock : update->stock);
}
for (const auto& patch : c.new_patches)
add(sum, patch.stock);
for (const auto& corpse : c.starved)
add(sum, corpse.inventory);
for (const auto& life : c.lives) {
add(sum, life.candidate.reserve_after);
add(sum, life.candidate.heat_after);
for (const auto& input : lives)
if (input.life_id == life.life_id)
add(sum, input.body);
}
sum.energy.value += c.heat_released.value;
add(sum, c.spent);
return sum;
}
Inventory before(const CellularHabitat& map, std::span<const CellularLifeStepInput> lives) {
Inventory sum{};
for (const auto& patch : map.patches())
add(sum, patch.stock);
for (const auto& life : lives) {
add(sum, life.reserve);
add(sum, life.body);
add(sum, life.heat);
}
return sum;
}
void collection_per_channel_with_a_shared_limit() {
const auto map = liquid_map(inv(1'000, 1'000, 1'000, 1'000, 1'000));
auto input = founder_input();
input.collection = {100, 200, 100, 100, 100};
input.energy_kept_per_thousand = 400;
const std::array lives{cell(1, {95, 95}, 1'000, {}, false, input)};
const auto result = propose_cellular_region_step(4, area, lives, *map, plain);
CHECK(result && result->lives.size() == 1);
const auto& reserve = result->lives[0].candidate.reserve_after;
// Carbon at 200 takes 100 of the shared 200; N and P take their 50; other matter gets none.
CHECK(reserve.matter[0].value == 100 && reserve.matter[1].value == 50 &&
reserve.matter[2].value == 50 && reserve.matter[3].value == 0);
// 50 pJ drained, 40% kept, the rest heat.
CHECK(reserve.energy.value == 20 && result->lives[0].candidate.heat_after.energy.value == 30);
// Food was left on every channel, so both limits held collection back.
CHECK(result->lives[0].candidate.matter_limited && result->lives[0].candidate.energy_limited);
CHECK(total(*result, *map, lives) == before(*map, lives));
}
void a_thicker_membrane_collects_slower() {
const auto map = liquid_map(inv(1'000, 1'000, 1'000, 1'000, 1'000));
auto input = founder_input();
input.membrane_nm = 1'100; // 110 points.
CellularRevampStepRules costly{};
costly.membrane_collection_per_mille_per_point = 20; // 10 points: -20%.
const CellularRegionRules rules{true, true, costly};
const std::array lives{cell(1, {95, 95}, 1'000, {}, false, input)};
const auto result = propose_cellular_region_step(4, area, lives, *map, rules);
CHECK(result);
const auto& reserve = result->lives[0].candidate.reserve_after;
CHECK(reserve.matter[0].value == 40 && reserve.energy.value == 40);
CHECK(total(*result, *map, lives) == before(*map, lives));
}
void matter_pays_upkeep_and_nothing_left_starves() {
const auto map = liquid_map({}, false);
// 20 pJ per second is 1 pJ per tick; 10 pJ per 1,000 fg makes that 100 fg of carbon.
const std::array lives{
cell(1, {500, 500}, 1'000, inv(0, 1'000, 0, 0, 0), false, founder_input(), 20),
cell(2, {900, 900}, 1'000, {}, false, founder_input(), 20)};
const auto result = propose_cellular_region_step(4, area, lives, *map, plain);
CHECK(result && result->lives.size() == 1 && result->lives[0].life_id == 1);
CHECK(result->lives[0].candidate.reserve_after == inv(0, 900, 0, 0, 0));
CHECK(result->lives[0].candidate.spent == inv(0, 100, 0, 0, 0));
CHECK(result->lives[0].candidate.heat_after.energy.value == 0); // Matter adds no heat.
CHECK(result->spent == inv(0, 100, 0, 0, 0));
CHECK(result->starved.size() == 1 && result->starved[0].life_id == 2);
CHECK(total(*result, *map, lives) == before(*map, lives));
}
void reserve_mass_and_capacity_decide_engulfment() {
const auto map = liquid_map({}, false);
// Predator 3,000; prey body 1,000 plus 1,000 fg of reserve matter: exactly 1.5 times.
const auto prey_reserve = inv(100, 250, 250, 250, 250);
{
const std::array lives{cell(1, {500, 500}, 3'000, {}, true),
cell(2, {510, 500}, 1'000, prey_reserve)};
const auto result = propose_cellular_region_step(4, area, lives, *map, plain);
CHECK(result && result->lives.size() == 1 &&
result->lives[0].engulf == CellularEngulfStatus::engulfed);
CHECK(total(*result, *map, lives) == before(*map, lives));
}
{
// One more femtogram of reserve makes the prey too large.
const std::array lives{cell(1, {500, 500}, 3'000, {}, true),
cell(2, {510, 500}, 1'000, inv(100, 251, 250, 250, 250))};
const auto result = propose_cellular_region_step(4, area, lives, *map, plain);
CHECK(result && result->lives.size() == 2 &&
result->lives[0].engulf == CellularEngulfStatus::prey_too_large);
// Counting no reserve, or a bigger chamber, makes it eligible again.
CellularRevampStepRules unladen{};
unladen.engulf_reserve_percent = 0;
const CellularRegionRules light{true, true, unladen};
const auto again = propose_cellular_region_step(4, area, lives, *map, light);
CHECK(again && again->lives.size() == 1);
auto chamber = founder_input();
chamber.engulf_capacity = 105;
const std::array bigger{cell(1, {500, 500}, 3'000, {}, true, chamber), lives[1]};
const auto wider = propose_cellular_region_step(4, area, bigger, *map, plain);
CHECK(wider && wider->lives.size() == 1);
}
{
// A thicker membrane defends: reported as the membrane, not the size.
auto tough = founder_input();
tough.membrane_nm = 1'200;
const std::array lives{cell(1, {500, 500}, 3'000, {}, true),
cell(2, {510, 500}, 1'000, prey_reserve, false, tough)};
const auto result = propose_cellular_region_step(4, area, lives, *map, plain);
CHECK(result && result->lives.size() == 2 &&
result->lives[0].engulf == CellularEngulfStatus::membrane_too_thick);
}
}
void a_kill_splits_its_remainder() {
const auto map = liquid_map({}, false);
auto hunter = founder_input();
hunter.energy_kept_per_thousand = 500;
hunter.kill_absorption = 200; // Twice the 50-per-tick limit: 100 per channel.
const std::array lives{cell(1, {500, 500}, 4'000, {}, true, hunter),
cell(2, {510, 500}, 1'000, inv(1'000, 290, 290, 290, 290))};
const auto result = propose_cellular_region_step(4, area, lives, *map, plain);
CHECK(result && result->lives.size() == 1);
const auto& predator = result->lives[0].candidate;
// Prey: 1,000 pJ and 300 fg per channel (290 reserve + 10 body). Absorbed: 100 each;
// half of the 100 pJ kept, half heat.
CHECK(predator.reserve_after == inv(50, 100, 100, 100, 100));
CHECK(predator.heat_after.energy.value == 50);
// Remainder 900 pJ and 200 fg per channel: half as bubbles, half to the spent pool.
CHECK(result->spent == inv(450, 100, 100, 100, 100));
Inventory bubbles{};
for (const auto& patch : result->new_patches)
add(bubbles, patch.stock);
CHECK(bubbles == inv(450, 100, 100, 100, 100));
CHECK(total(*result, *map, lives) == before(*map, lives));
}
void every_life_needs_a_revamp_input() {
const auto map = liquid_map({}, false);
auto bare = cell(1, {500, 500}, 1'000, {});
bare.revamp.reset();
const std::array lives{bare};
CHECK(!propose_cellular_region_step(4, area, lives, *map, plain));
auto greedy = founder_input();
greedy.energy_kept_per_thousand = 1'001;
const auto kept = cell(1, {500, 500}, 1'000, {}, false, greedy);
const std::array bad{kept};
CHECK(!propose_cellular_region_step(4, area, bad, *map, plain));
// And none without rules 13.
const std::array revamped{cell(1, {500, 500}, 1'000, {})};
CHECK(!propose_cellular_region_step(4, area, revamped, *map, CellularRegionRules{true, true}));
}
} // namespace
int main() {
try {
collection_per_channel_with_a_shared_limit();
a_thicker_membrane_collects_slower();
matter_pays_upkeep_and_nothing_left_starves();
reserve_mass_and_capacity_decide_engulfment();
a_kill_splits_its_remainder();
every_life_needs_a_revamp_input();
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
return 1;
}
std::puts("PASS rules 13 region step: collection, energy, matter as energy and engulfment");
return 0;
}