Files
aevolution/tests/cellular_liquid_owner_tests.cpp
T
dingoandClaude Opus 5.5 af32af36ee
Foundation / checks (checks) (push) Successful in 5s
Foundation / checks (client) (push) Successful in 5s
Foundation / checks (network) (push) Successful in 5s
Foundation / checks (postgres) (push) Successful in 4s
Foundation / checks (tsan) (push) Successful in 4s
Foundation / checks (ecs) (push) Successful in 1m6s
Foundation / checks (deploy) (push) Successful in 5m41s
Foundation / postgres-tools (push) Successful in 1m33s
Foundation / image (push) Successful in 4m42s
Foundation / server-image (push) Successful in 4m55s
feat(cellular): random founder spawns, tunable thrust cost, commit timing
Playtest feedback on region 10.

Rules-12 founders now start at a seeded random spot anywhere in the region (1 mm
clear of the edges, clear of other cells; seeded by world, region and the life's
identity so replay is exact). The provisioned sites remain the fallback.

Thrust energy is tunable: founder.thrust_cost_per_mille scales the metabolic
full-speed movement cost for thrust and braking (trial 250, a quarter of today's
cost). It is part of the liquid rules declared when a region opens, persisted in
the format-10 block's reserved slot (zero stands for the original 1,000), so
existing regions keep their meaning.

The server logs every cellular commit slower than 50 ms (and one in 60) with its
tick count and duration, to find what stalls the loop once a second.

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

231 lines
11 KiB
C++

#include "evo/server/lifecycle/cellular_batch.hpp"
#include "fixtures/cellular_ecology.hpp"
#include <algorithm>
#include <array>
#include <cstdio>
#include <vector>
// Rules 12 owner (Cellular_Region_Movement_Plan.md phase 2): the format 9 -> 10 upgrade, liquid
// motion inside the owner's tick, moving food and exact restore.
namespace {
using namespace evo;
using namespace evo::server;
using namespace evo::fixture;
constexpr biology::CellularArea area{48'000, 48'000};
CellularLiquidRules rules(std::uint64_t seed = 5) {
return {.currents = {seed, 48'000, 48'000, 250, 2, 4, 3'000, 6'000, 60, 300},
.founder = {.thrust_q10 = 20'480,
.reverse_thrust_q10 = 20'480,
.forward_drag_ppm = 95'163,
.lateral_drag_ppm = 95'163,
.turn = 3'276},
.deposits = {.drift_radius_um = 200,
.max_drift_q10 = 100 * 1'024,
.drift_decay_ppm = 95'163,
.merged_cap = 8,
.bubble_radius_um = biology::cellular_mote_radius_um}};
}
// A rules-12 region whose founder is born after the upgrade (founder 5, schema 6).
Owner liquid_region() {
auto state = opening(7, true, 1, false);
checked(state->advance_tick({}), "genesis tick");
state = checked(state->migration_upgrade_candidate(), "boundary upgrade");
state = checked(state->maturation_upgrade_candidate(), "maturation upgrade");
state = checked(state->inheritance_upgrade_candidate(), "inheritance upgrade");
state = checked(state->photosynthesis_upgrade_candidate({0, 1, {0}}), "light upgrade");
require(!state->liquid_upgrade_candidate(CellularLiquidRules{}), "empty rules accepted");
auto wrong = rules();
wrong.currents.width_um = 30'000;
require(!state->liquid_upgrade_candidate(wrong), "rules for another area accepted");
state = checked(state->liquid_upgrade_candidate(rules()), "liquid upgrade");
require(state->format_version() == cellular_lives_liquid_format &&
state->rules_version() == cellular_lives_liquid_rules &&
state->liquid_rules() == rules() && state->flow_field() != nullptr &&
state->habitat().config().format == biology::cellular_habitat_liquid_format &&
!state->liquid_upgrade_candidate(rules()),
"liquid upgrade repeated, wrong versions or lost rules");
for (const auto& patch : state->habitat().patches())
require(patch.merged == 1, "upgraded deposit without a merged count");
LifeAccount account{};
account.back() = 1;
state = checked(state->birth_candidate({account, 1}, {100, 100, 100, 100, {state->tick()}}),
"founder 5 birth");
const auto genome = state->archive().find(100);
require(genome && genome->schema_version() == biology::genome_schema_v6 &&
genome->movement_arrangement() == biology::founder_movement_arrangement,
"founder 5 is not schema 6 with the founder arrangement");
return state;
}
std::vector<CellularLifeIntent> swim(const CellularLives& state, biology::CellularIntent movement,
bool brake = false) {
std::vector<CellularLifeIntent> result;
for (const auto& life : state.lives()) {
CellularLifeIntent input{life.identity.life, {}};
input.intent.movement = movement;
input.intent.brake = brake;
result.push_back(input);
}
return result;
}
void closed(const CellularLives& state) {
const auto& ledger = state.boundary_ledger();
auto expected = state.opening_total();
expected.energy.value += ledger.incoming.energy.value - ledger.outgoing.energy.value;
for (std::size_t channel = 0; channel < expected.matter.size(); ++channel)
expected.matter[channel].value +=
ledger.incoming.matter[channel].value - ledger.outgoing.matter[channel].value;
require(state.current_total() == expected, "liquid region broke closure");
}
void upgrade_and_restore() {
auto state = liquid_region();
const auto copy = restore(*state);
require(copy->snapshot() == state->snapshot() && copy->liquid_rules() == rules() &&
copy->flow_field()->digest() == state->flow_field()->digest(),
"format 10 did not restore exactly");
}
void swimming_moves_and_pays() {
auto state = liquid_region();
const auto start = state->lives().front();
for (int tick = 0; tick < 40; ++tick)
checked(state->advance_tick(swim(*state, {1'000, 0})), "swim tick");
const auto& life = state->lives().front();
require(life.motion.velocity_x_q10 > 150 * 1'024, "the founder did not reach swimming speed");
require(life.position.x_um > start.position.x_um + 100, "the founder did not move east");
closed(*state);
// Braking brings the relative velocity to rest.
for (int tick = 0; tick < 40; ++tick)
checked(state->advance_tick(swim(*state, {}, true)), "brake tick");
require(state->lives().front().motion.velocity_x_q10 == 0 &&
state->lives().front().motion.velocity_y_q10 == 0,
"braking did not stop the founder relative to the liquid");
closed(*state);
}
void food_moves_and_merges_without_loss() {
auto state = liquid_region();
const auto before = state->habitat().config().patches;
for (int tick = 0; tick < 400; ++tick)
checked(state->advance_tick(swim(*state, {})), "drift tick");
const auto after = state->habitat().config().patches;
const auto moved = std::ranges::count_if(after, [&before](const biology::CellularPatch& p) {
const auto old = std::ranges::find(before, p.id, &biology::CellularPatch::id);
return old != before.end() && old->address != p.address;
});
require(moved > 0, "no bubble moved with the current");
for (const auto& patch : after)
if (patch.kind == biology::CellularDepositKind::pool)
require(std::ranges::any_of(before,
[&patch](const biology::CellularPatch& old) {
return old.id == patch.id &&
old.address == patch.address;
}),
"a pool moved");
closed(*state);
}
void restore_continues_exactly() {
auto continuous = liquid_region();
for (int tick = 0; tick < 30; ++tick)
checked(continuous->advance_tick(swim(*continuous, {600, 800})), "continuous tick");
auto resumed = restore(*continuous);
for (int tick = 0; tick < 30; ++tick) {
checked(continuous->advance_tick(swim(*continuous, {-700, 300})), "continuous tick");
checked(resumed->advance_tick(swim(*resumed, {-700, 300})), "resumed tick");
}
require(continuous->snapshot() == resumed->snapshot(), "restore diverged from a live run");
}
// A trace of swimming and braking replays to the exact live result (brake is flag bit 1).
void trace_replays_brakes() {
auto live = liquid_region();
const auto base = restore(*live);
CellularBatch batch;
batch.format_version = cellular_batch_inheritance_format;
batch.header = {live->world(), live->region(), live->frame(), {}, {}, live->tick(), 0};
batch.header.predecessor.fill(1); // Any bound commit; this test replays portably.
batch.header.predecessor_digest.fill(2);
for (int tick = 0; tick < 24; ++tick) {
const auto intents =
swim(*live, {static_cast<std::int16_t>(tick < 12 ? 1'000 : 0), 0}, tick >= 12);
checked(live->advance_tick(intents), "recorded tick");
batch.ticks.push_back({intents, {}, {}});
}
batch.header.end_tick = live->tick();
const auto bytes = checked(encode_cellular_batch(batch), "brake trace encodes");
const auto decoded = checked(decode_cellular_batch(bytes), "brake trace decodes");
require(decoded == batch && decoded.ticks.back().lives.front().intent.brake,
"the brake did not survive the trace");
const auto replayed = checked(replay_cellular_batch(*base, decoded), "brake trace replays");
require(replayed->snapshot() == live->snapshot(), "replay diverged from the live run");
}
// Founders start at a seeded random spot, not a provisioned site, identically on replay.
void founders_spawn_at_random() {
const auto state = liquid_region();
const auto first = state->lives().front().position;
const auto sites = std::array{1'000ULL, 4'000ULL};
require(!(std::ranges::find(sites, first.x_um) != sites.end() &&
std::ranges::find(sites, first.y_um) != sites.end()),
"a rules-12 founder started on a provisioned site");
require(first.x_um >= 1'000 && first.y_um >= 1'000 && first.x_um < 47'000 &&
first.y_um < 47'000,
"a founder started too close to the region edge");
// The same identity starts at the same spot (the fixture funds one founder).
auto again = liquid_region();
require(again->lives().front().position == first, "spawning is not deterministic");
}
// The declared thrust cost share scales every thrust and braking cost.
void thrust_cost_is_tunable() {
const auto state = liquid_region();
const auto& life = state->lives().front();
const auto genome = state->archive().find(life.identity.genome);
require(genome.has_value(), "founder genome missing");
const auto budget = biology::metabolic_budget(*genome, life.growth, {293'150, {}});
require(budget.has_value(), "founder budget");
auto full = rules();
auto quarter = rules();
quarter.thrust_cost_per_mille = 250;
const auto a = checked(cellular_liquid_handling(*genome, *budget, full), "full cost");
const auto b = checked(cellular_liquid_handling(*genome, *budget, quarter), "quarter cost");
require(a.thrust_cost.value > 0 &&
b.thrust_cost.value == (a.thrust_cost.value * 250 + 999) / 1'000 &&
b.thrust_q10 == a.thrust_q10,
"thrust cost did not scale");
auto zero = rules();
zero.thrust_cost_per_mille = 0;
require(!cellular_liquid_valid(zero, area), "a zero thrust cost share was accepted");
}
void older_rules_refuse_brake() {
auto state = opening(7, true, 1);
require(!state->advance_tick(swim(*state, {}, true)), "rules 8 accepted a brake");
}
} // namespace
int main() {
try {
upgrade_and_restore();
swimming_moves_and_pays();
food_moves_and_merges_without_loss();
restore_continues_exactly();
trace_replays_brakes();
founders_spawn_at_random();
thrust_cost_is_tunable();
older_rules_refuse_brake();
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
return 1;
}
std::puts("cellular liquid owner contracts passed");
return 0;
}