Files
aevolution/tests/cellular_revamp_tuning_tests.cpp
T
dingoandClaude Opus 5.5 76d21b4815
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 4m29s
Foundation / postgres-tools (push) Successful in 31s
Foundation / server-image (push) Successful in 4m24s
Foundation / image (push) Successful in 5m3s
feat(cellular): rules 13 feeding, matter as energy, spent pool and predation
Phase 2 of the cellular revamp build plan (ADR-0179), server half.

- Matter as energy: when an energy cost exceeds the reserve's energy, reserve
  matter stands in for the rest at each channel's burn rate (pJ per 1,000 fg,
  scaled by the genome's burn efficiency), largest channel first. Upkeep may
  burn any channel; thrust only channels the player has not excluded. Burned
  matter adds no heat; it goes to the region's spent pool. A cell dies only when
  nothing can pay its upkeep.
- Collection: per-channel limits scaled by the genome's collection indices, a
  shared limit on all matter, a tunable cost per membrane point, and only the
  genome's kept share (energy conversion) of bubble energy reaching the reserve.
- Predation: engulf mass carries a tunable share of reserve matter, the
  predator's engulf capacity scales its side and the prey's membrane (now in
  nanometres) its own; kill absorption scales the predator's intake; the kill's
  remainder is split between bubbles at the prey and the spent pool, and a full
  habitat sends overflow to the pool.
- The spent pool is owner format 11 state, counted in conservation, and drips
  back as seeded random bubbles every interval tick.
- Reserve matter is carried as load: slower thrust and turning, costlier thrust.
- Channel exclusions are a 4-bit step intent field, trace byte 14; only owned
  lives under rules 13 may send them.

All values come from the revamp tuning file. Rules up to 12 are unchanged. Tests
cover payments and their conservation over 400 seeds, the rules-13 region step
(collection, membrane, kept energy, burning, engulfment and kill splits with
closure), the drip, load, exclusions, a 700-tick run with closure and exact
restore, and trace replay with exclusions. Session v14 and the client follow
the server phases (build plan). Refreshes the DEV session window.

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

113 lines
4.6 KiB
C++

#include "evo/biology/cellular_revamp_tuning.hpp"
#include <cstdio>
#include <fstream>
#include <iterator>
#include <source_location>
#include <stdexcept>
#include <string>
// The rules-13 revamp tuning file (ADR-0179): same format and refusals as the rules-12 file,
// with its own keys.
namespace {
using namespace evo::biology;
void check(bool condition, std::source_location at = std::source_location::current()) {
if (!condition)
throw std::runtime_error("cellular revamp tuning line " + std::to_string(at.line()));
}
#define CHECK(expression) check(static_cast<bool>(expression))
CellularRevampTuning trial() {
return {.version = 1,
.body_essential_upkeep_per_mille = 250,
.body_reserve_load_per_mille = 1'000,
.energy_burn_carbon_pj_per_kfg = 2'000,
.energy_burn_nitrogen_pj_per_kfg = 2'000,
.energy_burn_phosphorus_pj_per_kfg = 2'000,
.energy_burn_other_pj_per_kfg = 2'000,
.feeding_membrane_collection_per_mille_per_point = 2,
.predation_engulf_ratio_milli = 1'500,
.predation_engulf_reserve_percent = 100,
.predation_kill_bubble_per_mille = 500,
.spent_drip_interval_ticks = 20,
.spent_drip_bubble_pj = 20'000'000,
.spent_drip_bubble_fg = 1'000'000};
}
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_revamp_tuning(text);
check(!parsed.has_value());
return parsed.error().kind;
}
void canonical_round_trip() {
const auto text = encode_cellular_revamp_tuning(trial());
const auto parsed = parse_cellular_revamp_tuning(text);
CHECK(parsed && *parsed == trial());
CHECK(encode_cellular_revamp_tuning(*parsed) == text);
const auto commented =
"# heading\n\n" + with(text, "body.essential_upkeep_per_mille", "2_50 # c");
CHECK(parse_cellular_revamp_tuning(commented) == trial());
}
void refusals() {
const auto base = encode_cellular_revamp_tuning(trial());
CHECK(error_of(base + "unknown.key = 1\n") == CellularTuningErrorKind::unknown_key);
CHECK(error_of(base + "tuning.version = 1\n") == CellularTuningErrorKind::duplicate_key);
CHECK(error_of(base + "no equals sign\n") == CellularTuningErrorKind::syntax);
CHECK(error_of(with(base, "body.essential_upkeep_per_mille", "1001")) ==
CellularTuningErrorKind::out_of_range);
CHECK(error_of(with(base, "body.essential_upkeep_per_mille", "x")) ==
CellularTuningErrorKind::invalid_value);
CHECK(error_of(with(base, "tuning.version", "2")) ==
CellularTuningErrorKind::unsupported_version);
auto missing = base;
missing.erase(missing.find("body.essential_upkeep_per_mille"));
const auto parsed = parse_cellular_revamp_tuning(missing);
CHECK(!parsed && parsed.error().kind == CellularTuningErrorKind::missing_key &&
parsed.error().key == "body.essential_upkeep_per_mille");
CHECK(error_of(with(base, "spent.drip_bubble_fg", "0")) ==
CellularTuningErrorKind::inconsistent);
CHECK(error_of(with(base, "predation.engulf_reserve_percent", "101")) ==
CellularTuningErrorKind::out_of_range);
// Rules-12 keys belong in the other file.
CHECK(error_of(base + "region.width_um = 60000\n") == CellularTuningErrorKind::unknown_key);
CHECK(error_of(std::string(max_cellular_revamp_tuning_bytes + 1, '#')) ==
CellularTuningErrorKind::too_large);
}
// The checked-in DEV revamp 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_revamp_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 == trial());
}
} // namespace
int main(int argc, char** argv) {
try {
canonical_round_trip();
refusals();
if (argc > 1)
repository_file(argv[1]);
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
return 1;
}
std::puts("PASS cellular revamp tuning file");
return 0;
}