Files
aevolution/tests/cellular_tuning_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

240 lines
10 KiB
C++

#include "evo/accounting/rates.hpp"
#include "evo/biology/cellular_tuning.hpp"
#include "evo/biology/founder.hpp"
#include <cstdio>
#include <fstream>
#include <iterator>
#include <source_location>
#include <stdexcept>
#include <string>
namespace {
using namespace evo::biology;
void check(bool value, std::source_location at = std::source_location::current()) {
if (!value)
throw std::runtime_error("cellular tuning line " + std::to_string(at.line()));
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
CellularTuning v0() {
return {.version = 1,
.region_width_um = 60'000,
.region_height_um = 60'000,
.region_organism_cap = 2'000,
.region_deposit_limit = 8'192,
.region_perception_radius_um = 5'000,
.founder_thrust_um_per_s2 = 400,
.founder_reverse_thrust_um_per_s2 = 400,
.founder_drag_ppm_per_tick = 95'163,
.founder_lateral_drag_ppm_per_tick = 95'163,
.founder_turn_heading_per_s = 65'536,
.founder_thrust_cost_per_mille = 250,
.currents_min_count = 2,
.currents_max_count = 4,
.currents_min_width_um = 3'000,
.currents_max_width_um = 6'000,
.currents_min_speed_um_per_s = 60,
.currents_max_speed_um_per_s = 300,
.currents_grid_pitch_um = 250,
.food_merged_bubble_cap_multiple = 8,
.food_visible_moving_bubbles = 256,
.food_induced_drift_radius_um = 200,
.food_induced_drift_max_um_per_s = 100,
.food_induced_drift_decay_ppm_per_tick = 95'163,
.thermal_ambient_min_mk = 278'150,
.thermal_ambient_max_mk = 313'150,
.founder_preferred_mk = 293'150,
.founder_comfort_band_mk = 5'000,
.founder_body_tau_ms = 20'000,
.founder_lethal_cold_mk = 273'150,
.founder_lethal_heat_mk = 318'150,
.thermal_off_preference_per_mille_per_k = 80,
.thermal_transition_per_mille_per_k = 40,
.thermal_multiplier_cap_per_mille = 8'000,
.lifecycle_logout_delay_ms = 10'000,
.mutation_step_points = 5,
.mutation_paired_points = 2,
.mutation_recovery_floor_per_mille = 100,
.mutation_offer_interval_ms = 25'000,
.mutation_offer_options = 3};
}
// Replaces the first `key = ...` line of canonical text.
std::string with(std::string text, const std::string& key, const std::string& value) {
const auto at = text.find(key + " = ");
check(at != std::string::npos);
const auto end = text.find('\n', at);
return text.replace(at, end - at, key + " = " + value);
}
CellularTuningErrorKind error_of(const std::string& text) {
const auto parsed = parse_cellular_tuning(text);
check(!parsed.has_value());
return parsed.error().kind;
}
void canonical_round_trip() {
const auto text = encode_cellular_tuning(v0());
const auto parsed = parse_cellular_tuning(text);
CHECK(parsed && *parsed == v0());
CHECK(encode_cellular_tuning(*parsed) == text);
}
void comments_separators_and_whitespace() {
auto text = "# heading\n\n" + encode_cellular_tuning(v0());
text = with(text, "region.width_um", "60_000 # trailing comment\r");
text = with(text, "region.height_um", "\t60000");
const auto parsed = parse_cellular_tuning(text);
CHECK(parsed && *parsed == v0());
}
void refusals() {
const auto base = encode_cellular_tuning(v0());
CHECK(error_of(base + "unknown.key = 1\n") == CellularTuningErrorKind::unknown_key);
CHECK(error_of(base + "region.width_um = 60000\n") == CellularTuningErrorKind::duplicate_key);
CHECK(error_of(base + "no equals sign\n") == CellularTuningErrorKind::syntax);
CHECK(error_of(base + "Region.Width = 1\n") == CellularTuningErrorKind::syntax);
CHECK(error_of(with(base, "region.width_um", "60k")) == CellularTuningErrorKind::invalid_value);
CHECK(error_of(with(base, "region.width_um", "60__000")) ==
CellularTuningErrorKind::invalid_value);
CHECK(error_of(with(base, "region.width_um", "")) == CellularTuningErrorKind::invalid_value);
CHECK(error_of(with(base, "region.width_um", "999")) == CellularTuningErrorKind::out_of_range);
CHECK(error_of(with(base, "founder.drag_ppm_per_tick", "1000000")) ==
CellularTuningErrorKind::out_of_range);
CHECK(error_of(with(base, "region.width_um", "# missing")) ==
CellularTuningErrorKind::invalid_value);
CHECK(error_of(with(base, "currents.min_count", "5")) == CellularTuningErrorKind::inconsistent);
CHECK(error_of(with(base, "founder.preferred_mk", "320000")) ==
CellularTuningErrorKind::inconsistent);
CHECK(error_of(with(base, "tuning.version", "2")) ==
CellularTuningErrorKind::unsupported_version);
auto missing = base;
missing.erase(missing.find("mutation.offer_options"));
const auto parsed = parse_cellular_tuning(missing);
CHECK(!parsed && parsed.error().kind == CellularTuningErrorKind::missing_key &&
parsed.error().key == "mutation.offer_options");
const auto line = parse_cellular_tuning(base + "\nbad\n");
CHECK(!line && line.error().line == 42);
CHECK(error_of(std::string(max_cellular_tuning_bytes + 1, '#')) ==
CellularTuningErrorKind::too_large);
}
void founder_handling() {
const auto handling = founder_cellular_handling(v0(), {1'000}, {900});
CHECK(handling.thrust_q10 == 20'480 && handling.reverse_thrust_q10 == 20'480);
CHECK(handling.forward_drag_ppm == 95'163 && handling.lateral_drag_ppm == 95'163);
CHECK(handling.turn == 3'276);
CHECK(handling.thrust_cost.value == 1'000 && handling.reverse_cost.value == 900);
}
Genome founder_genome(const MovementArrangement& arrangement, bool movement = true) {
const auto founder = primitive_founder(1, 1, primitive_founder_v5);
check(founder.has_value());
GenomeDraft draft{2, 2, 6, {1}, {}, genome_schema_v6, arrangement};
for (const auto& record : founder->capabilities())
if (movement || record.kind != Capability::movement)
draft.capabilities.push_back(record);
const auto genome = Genome::create(draft);
check(genome.has_value());
return *genome;
}
CellularHandling handling_of(const Genome& genome) {
const auto budget = metabolic_budget(genome, {1'000, 0}, {293'150, {}});
check(budget.has_value());
const auto handling = derive_cellular_handling(genome, *budget, v0());
check(handling.has_value());
return *handling;
}
void derived_handling() {
const auto founder = primitive_founder(1, 1, primitive_founder_v5);
CHECK(founder && founder->movement_arrangement() == founder_movement_arrangement);
const auto budget = metabolic_budget(*founder, {1'000, 0}, {293'150, {}});
CHECK(budget);
const auto base = handling_of(*founder);
// The founder arrangement reproduces the tuned founder handling exactly.
const auto full =
*evo::accounting::per_tick(budget->movement.value, 50'000, evo::accounting::Rounding::up);
CHECK(base == founder_cellular_handling(v0(), {full}, {full}));
CHECK(full > 0);
auto a = founder_movement_arrangement;
a.output = 105; // 001: more thrust, costing more per tick.
const auto strong = handling_of(founder_genome(a));
CHECK(strong.thrust_q10 == 21'504 && strong.thrust_cost.value > base.thrust_cost.value);
CHECK(strong.reverse_thrust_q10 == base.reverse_thrust_q10);
a = founder_movement_arrangement;
a.efficiency = 105; // 002: same thrust, cheaper.
const auto economical = handling_of(founder_genome(a));
CHECK(economical.thrust_q10 == base.thrust_q10 &&
economical.thrust_cost.value < base.thrust_cost.value);
a = founder_movement_arrangement;
a.steering = 105; // 004
CHECK(handling_of(founder_genome(a)).turn == 3'439);
a = founder_movement_arrangement;
a.streamlining = 105; // 005: forward drag only.
const auto straight = handling_of(founder_genome(a));
CHECK(straight.forward_drag_ppm == 90'631 && straight.lateral_drag_ppm == 95'163);
a = founder_movement_arrangement;
a.braking = 105; // 006
CHECK(handling_of(founder_genome(a)).reverse_thrust_q10 == 21'504);
a = founder_movement_arrangement;
a.coasting = 105; // 008: both drags at 100/105, about 95.24%.
const auto coaster = handling_of(founder_genome(a));
CHECK(coaster.forward_drag_ppm == 90'631 && coaster.lateral_drag_ppm == 90'631);
a = founder_movement_arrangement;
a.output = 0; // 009: shed propulsion drifts but cannot thrust or turn.
const auto shed = handling_of(founder_genome(a));
CHECK(shed.thrust_q10 == 0 && shed.reverse_thrust_q10 == 0 && shed.turn == 0);
CHECK(shed.thrust_cost.value == 0 && shed.forward_drag_ppm == base.forward_drag_ppm);
CHECK(handling_of(founder_genome(founder_movement_arrangement, false)).thrust_q10 == 0);
const auto old = primitive_founder(1, 1, primitive_founder_v4);
CHECK(old);
const auto old_budget = metabolic_budget(*old, {1'000, 0}, {293'150, {}});
// Genomes from before schema 6 handle as founders.
CHECK(old_budget);
const auto old_handling = derive_cellular_handling(*old, *old_budget, v0());
CHECK(old_handling && old_handling->thrust_q10 == base.thrust_q10 &&
old_handling->forward_drag_ppm == base.forward_drag_ppm);
}
// The checked-in DEV tuning file must parse; CTest passes its path when it is present.
void repository_file(const char* path) {
std::ifstream file(path, std::ios::binary);
CHECK(file.good());
const std::string text{std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>()};
const auto parsed = parse_cellular_tuning(text);
if (!parsed)
std::fprintf(stderr, "%s: line %zu %s: %s\n", path, parsed.error().line,
parsed.error().key.c_str(),
std::string{describe(parsed.error().kind)}.c_str());
CHECK(parsed && *parsed == v0());
}
} // namespace
int main(int argc, char** argv) {
try {
canonical_round_trip();
comments_separators_and_whitespace();
refusals();
founder_handling();
derived_handling();
if (argc > 1)
repository_file(argv[1]);
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
return 1;
}
std::puts("cellular tuning contracts passed");
return 0;
}