Files
aevolution/tests/cellular_region_step_tests.cpp
dingoandClaude Opus 5.5 7c16a13dd9
Foundation / checks (ecs) (push) Successful in 2m24s
Foundation / checks (deploy) (push) Successful in 4m7s
Foundation / checks (tsan) (push) Successful in 9m49s
Foundation / checks (client) (push) Successful in 17m12s
Foundation / checks (network) (push) Successful in 19m34s
Foundation / checks (checks) (push) Successful in 43m5s
Foundation / checks (postgres) (push) Successful in 44m57s
Foundation / server-image (push) Successful in 31s
Foundation / postgres-tools (push) Successful in 1m13s
Foundation / image (push) Successful in 3m24s
Show what a mutation costs and why an engulf failed
After the first M11.9 playtest (ADR-0129 amendment): each offered candidate now
carries its funded cost to the cell, construction energy and matter per channel,
so the client can grey out what the reserve cannot pay; a candidate is affordable
by that cost exactly when the owner would fund it. A failed engulf now reports no
contact, prey too large, or membrane too thick. The v10 candidate record grows to
64 bytes, so v10 clients must be rebuilt.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 15:39:35 -04:00

251 lines
12 KiB
C++

#include "evo/biology/cellular_region_step.hpp"
#include <array>
#include <cstdio>
#include <source_location>
#include <stdexcept>
#include <string>
#include <utility>
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 region step line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
MetabolicBudget budget() {
MetabolicBudget value{};
value.rate_microseconds = 1'000'000;
value.intake_limit = {{1'000}, {{{1'000}, {1'000}, {1'000}, {1'000}}}};
return value;
}
std::unique_ptr<CellularHabitat> habitat() {
CellularHabitatConfig opening{7, 9, 11,
3'000, 2'000, {{{0, 0}, 51, {{5}, {{{2}, {2}, {2}, {2}}}}}}};
auto made = CellularHabitat::create(std::move(opening));
CHECK(made);
return std::move(*made);
}
void shared_patch_claims_are_identity_ordered_and_conserved() {
const auto map = habitat();
const CellularLifeStepInput later{20, {0, 0}, 20, {{0, 0}, CellularPatchAddress{0, 0}},
{}, {{100}, {}}, {}, budget()};
const CellularLifeStepInput earlier{10, {0, 0}, 20, {{0, 0}, CellularPatchAddress{0, 0}},
{}, {{100}, {}}, {}, budget()};
const std::array input{later, earlier}; // Deliberately opposite the arbitration order.
const auto candidate = propose_cellular_region_step(4, {3'000, 2'000}, input, *map);
CHECK(candidate);
CHECK(candidate->begin_tick == 4 && candidate->end_tick == 5);
CHECK(candidate->lives.size() == 2);
CHECK(candidate->lives[0].life_id == 10 && candidate->lives[1].life_id == 20);
CHECK(candidate->lives[0].candidate.intake_status == CellularIntakeStatus::acquired);
const auto first_intake =
candidate->lives[0].candidate.intake.value_or(CellularPatchUptakeProposal{});
CHECK(candidate->lives[0].candidate.intake.has_value());
CHECK(first_intake.transfer.acquired == Inventory{{5}, {{{2}, {2}, {2}, {2}}}});
CHECK(candidate->lives[1].candidate.intake_status == CellularIntakeStatus::acquired);
const auto second_intake =
candidate->lives[1].candidate.intake.value_or(CellularPatchUptakeProposal{});
CHECK(candidate->lives[1].candidate.intake.has_value());
CHECK(second_intake.transfer.acquired == Inventory{});
CHECK(candidate->patches.size() == 1);
CHECK(candidate->patches[0] == CellularPatchStockUpdate{{0, 0}, {}});
CHECK(map->patches()[0].stock == Inventory{{5}, {{{2}, {2}, {2}, {2}}}});
Inventory matter{};
for (const auto& life : candidate->lives)
for (std::size_t channel = 0; channel < matter.matter.size(); ++channel)
matter.matter[channel].value += life.candidate.reserve_after.matter[channel].value;
for (std::size_t channel = 0; channel < matter.matter.size(); ++channel)
CHECK(matter.matter[channel].value + candidate->patches[0].stock.matter[channel].value ==
map->patches()[0].stock.matter[channel].value);
CHECK(candidate->lives[0].candidate.reserve_after.energy.value +
candidate->lives[1].candidate.reserve_after.energy.value +
candidate->patches[0].stock.energy.value ==
100 + 100 + map->patches()[0].stock.energy.value);
}
void refusal_never_returns_partial_tick_state() {
const auto map = habitat();
const std::array duplicate{
CellularLifeStepInput{1, {0, 0}, 20, {}, {}, {{100}, {}}, {}, budget()},
CellularLifeStepInput{1, {0, 0}, 20, {}, {}, {{100}, {}}, {}, budget()}};
const auto repeated = propose_cellular_region_step(4, {3'000, 2'000}, duplicate, *map);
CHECK(!repeated && repeated.error() == CellularRegionStepError::duplicate_life_id);
auto starved_life = CellularLifeStepInput{
2, {0, 0}, 20, {}, {{0}, {{{1}, {2}, {3}, {4}}}}, {{7}, {{{1}, {1}, {1}, {1}}}},
{{17}, {}}, budget()};
auto starved_budget = budget();
starved_budget.maintenance = {1'000};
starved_life.budget = starved_budget;
const std::array starved{starved_life};
const auto result = propose_cellular_region_step(4, {3'000, 2'000}, starved, *map);
CHECK(result);
CHECK(result->begin_tick == 4 && result->end_tick == 5);
CHECK(result->lives.empty() && result->starved.size() == 1);
CHECK(result->starved[0] == CellularCorpseCandidate{2, {0, 0}, {{7}, {{{2}, {3}, {4}, {5}}}}});
CHECK(result->heat_released.value == 17);
CHECK(result->patches.empty());
CHECK(map->patches()[0].stock == Inventory{{5}, {{{2}, {2}, {2}, {2}}}});
}
// ADR-0124 (rules 3) helpers: a cell of the given total mass, membrane and engulf intent.
CellularLifeStepInput cell(std::uint64_t id, CellularPosition at, std::uint64_t mass,
std::uint32_t membrane, bool engulf) {
CellularLifeStepInput life{id, at, 20, {}, {{0}, {{{10}, {10}, {10}, {10}}}},
{{100}, {}}, {}, budget()};
life.budget.phenotype.total_mass = mass;
life.intent.engulf = engulf;
life.membrane_um = membrane;
life.source = 900 + id;
return life;
}
constexpr CellularRegionRules rules3{true};
Inventory total(const CellularRegionStepCandidate& c, const CellularHabitat& map) {
Inventory sum{};
const auto add = [&](const Inventory& v) {
sum.energy.value += v.energy.value;
for (std::size_t i = 0; i < v.matter.size(); ++i)
sum.matter[i].value += v.matter[i].value;
};
std::vector<bool> updated(map.patches().size(), false);
for (const auto& patch : c.patches) {
add(patch.stock);
const auto index = map.find(patch.address);
CHECK(index.has_value());
if (index)
updated[*index] = true;
}
for (std::size_t i = 0; i < updated.size(); ++i)
if (!updated[i])
add(map.patches()[i].stock);
for (const auto& patch : c.new_patches)
add(patch.stock);
for (const auto& corpse : c.starved)
add(corpse.inventory);
sum.energy.value += c.heat_released.value;
return sum;
}
void engulfment_moves_the_prey_once_bounded_by_capacity() {
const auto map = habitat();
// Predator 3x the prey's mass, prey with a 1 um membrane: 3 >= 1.5.
const std::array input{cell(2, {0, 0}, 1'000, 1, false), cell(1, {10, 0}, 3'000, 1, true)};
const auto result = propose_cellular_region_step(4, {3'000, 2'000}, input, *map, rules3);
CHECK(result);
CHECK(result->lives.size() == 1 && result->lives[0].life_id == 1);
CHECK(result->lives[0].engulf == CellularEngulfStatus::engulfed &&
result->lives[0].prey_id == 2);
CHECK(result->starved.size() == 1 && result->starved[0].life_id == 2 &&
result->starved[0].cause == CellularDeathCause::engulfed &&
result->starved[0].predator_id == 1 && result->starved[0].inventory == Inventory{});
// Capacity is the per-tick intake limit: 1,000 per second is 50 per 50 ms tick.
// The prey holds 100 pJ and 10 fg per channel; the predator takes 50 pJ and all matter.
const auto& predator = result->lives[0].candidate.reserve_after;
CHECK(predator.energy.value == 100 + 50);
CHECK(predator.matter[0].value == 10);
// The other 50 pJ is detritus in the patch at the prey's cell.
CHECK(result->patches.size() == 1 &&
result->patches[0] == CellularPatchStockUpdate{{0, 0}, {{55}, {{{2}, {2}, {2}, {2}}}}});
CHECK(result->new_patches.empty());
// Region-wide conservation: lives + patches + corpses + released heat.
auto after = total(*result, *map);
after.energy.value += predator.energy.value;
for (std::size_t i = 0; i < after.matter.size(); ++i)
after.matter[i].value += predator.matter[i].value + 10; // The predator's body.
CHECK(after == Inventory{{5 + 100 + 100}, {{{2 + 20}, {2 + 20}, {2 + 20}, {2 + 20}}}});
}
void size_and_membrane_contest_engulfment() {
const auto map = habitat();
const std::array equal{cell(1, {0, 0}, 1'000, 1, true), cell(2, {0, 0}, 1'000, 1, false)};
const auto same = propose_cellular_region_step(4, {3'000, 2'000}, equal, *map, rules3);
CHECK(same && same->lives.size() == 2 && same->starved.empty());
// The owner learns why: equal sizes are too large whatever the membrane.
CHECK(same->lives[0].engulf == CellularEngulfStatus::prey_too_large);
// A 3 um membrane raises the required ratio to 4.5; a 3x predator fails.
const std::array armored{cell(1, {0, 0}, 3'000, 1, true), cell(2, {0, 0}, 1'000, 3, false)};
const auto defended = propose_cellular_region_step(4, {3'000, 2'000}, armored, *map, rules3);
CHECK(defended && defended->lives.size() == 2 && defended->starved.empty());
// 3x would beat a 1 um membrane, so the membrane is the reason.
CHECK(defended->lives[0].engulf == CellularEngulfStatus::membrane_too_thick);
// Out of contact: no prey, whatever the size.
const std::array apart{cell(1, {0, 0}, 9'000, 1, true), cell(2, {2'000, 0}, 1'000, 1, false)};
const auto distant = propose_cellular_region_step(4, {3'000, 2'000}, apart, *map, rules3);
CHECK(distant && distant->lives.size() == 2 &&
distant->lives[0].engulf == CellularEngulfStatus::no_contact);
}
void a_taken_cell_neither_acts_nor_is_taken_twice() {
const auto map = habitat();
// 1 takes 2 first (ascending id), so 2's own engulf of 3 never happens and 4 cannot
// take 2 again; 4 takes 3 instead.
const std::array input{cell(1, {0, 0}, 9'000, 1, true), cell(2, {0, 0}, 3'000, 1, true),
cell(3, {0, 0}, 1'000, 1, false), cell(4, {0, 0}, 9'000, 1, true)};
const auto result = propose_cellular_region_step(4, {3'000, 2'000}, input, *map, rules3);
CHECK(result && result->lives.size() == 2);
CHECK(result->lives[0].life_id == 1 && result->lives[0].prey_id == 2);
CHECK(result->lives[1].life_id == 4 && result->lives[1].prey_id == 3);
CHECK(result->starved.size() == 2 && result->starved[0].predator_id == 1 &&
result->starved[1].predator_id == 4);
}
void starvation_detritus_creates_a_patch_at_an_empty_cell() {
const auto map = habitat();
auto starving = cell(5, {1'500, 1'500}, 1'000, 1, false);
starving.budget.maintenance = {1'000'000};
const std::array input{starving};
const auto result = propose_cellular_region_step(4, {3'000, 2'000}, input, *map, rules3);
CHECK(result && result->lives.empty() && result->starved.size() == 1);
CHECK(result->starved[0].cause == CellularDeathCause::starved &&
result->starved[0].inventory == Inventory{});
CHECK(result->new_patches.size() == 1);
CHECK(result->new_patches[0] ==
CellularPatch{{1'000, 1'000}, 905, {{100}, {{{10}, {10}, {10}, {10}}}}});
CHECK(result->patches.empty());
}
void rules_two_ignores_engulf_and_rules_three_refuses_bad_intents() {
const auto map = habitat();
const std::array input{cell(1, {0, 0}, 9'000, 1, true), cell(2, {0, 0}, 1'000, 1, false)};
const auto legacy = propose_cellular_region_step(4, {3'000, 2'000}, input, *map);
CHECK(legacy && legacy->lives.size() == 2 && legacy->starved.empty() &&
legacy->lives[0].engulf == CellularEngulfStatus::unsupported);
auto both = cell(1, {0, 0}, 9'000, 1, true);
both.intent.intake_target = CellularPatchAddress{0, 0};
const std::array conflicting{both};
const auto refused = propose_cellular_region_step(4, {3'000, 2'000}, conflicting, *map, rules3);
CHECK(!refused && refused.error() == CellularRegionStepError::invalid_intent);
const std::array bare{cell(1, {0, 0}, 9'000, 0, false)};
const auto unmembraned = propose_cellular_region_step(4, {3'000, 2'000}, bare, *map, rules3);
CHECK(!unmembraned && unmembraned.error() == CellularRegionStepError::invalid_intent);
}
} // namespace
int main() {
try {
shared_patch_claims_are_identity_ordered_and_conserved();
refusal_never_returns_partial_tick_state();
engulfment_moves_the_prey_once_bounded_by_capacity();
size_and_membrane_contest_engulfment();
a_taken_cell_neither_acts_nor_is_taken_twice();
starvation_detritus_creates_a_patch_at_an_empty_cell();
rules_two_ignores_engulf_and_rules_three_refuses_bad_intents();
std::puts("PASS cellular region step: shared claims, starvation, engulfment, detritus");
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
return 1;
}
}