Add cellular genome revision profile migration
Foundation / checks (deploy) (push) Successful in 5s
Foundation / checks (ecs) (push) Successful in 1m59s
Foundation / checks (checks) (push) Failing after 2m10s
Foundation / checks (postgres) (push) Failing after 10m34s
Foundation / checks (network) (push) Failing after 12m19s
Foundation / checks (tsan) (push) Failing after 2m14s
Foundation / checks (client) (push) Successful in 15m7s
Foundation / postgres-tools (push) Skipped
Foundation / server-image (push) Skipped
Foundation / image (push) Skipped

This commit is contained in:
dingo
2026-10-11 06:13:17 -04:00
parent 66ffdc1cad
commit a479ce2d95
25 changed files with 725 additions and 79 deletions
+59 -18
View File
@@ -12,6 +12,7 @@ namespace evo::server {
namespace {
constexpr std::uint64_t cellular_lives_v1_magic = 0x3130564C43564541ULL; // AEVCLV01
constexpr std::uint64_t cellular_lives_v2_magic = 0x3230564C43564541ULL; // AEVCLV02
constexpr std::uint64_t cellular_lives_v3_magic = 0x3330564C43564541ULL; // AEVCLV03
constexpr std::size_t cellular_lives_header_bytes = 212;
constexpr std::size_t cellular_site_bytes = 8;
constexpr std::size_t cellular_life_v1_bytes = 168;
@@ -222,8 +223,9 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
lives[j].identity.organism == life.identity.organism)
return std::unexpected{CellularLivesError::duplicate_life};
const auto record = archive.organism(life.identity.organism);
if (!record || !record->alive || record->genome != life.identity.genome ||
record->born != life.identity.born)
const auto current_genome = archive.current_genome(life.identity.organism);
if (!record || !record->alive || !current_genome ||
*current_genome != life.identity.genome || record->born != life.identity.born)
return std::unexpected{CellularLivesError::archive};
const auto budget = budget_for(archive, life, config.ambient_temperature_millikelvin);
if (!budget || budget->body != life.body || life.body.energy.value != 0 ||
@@ -248,7 +250,10 @@ validate_state(std::uint64_t world, std::uint64_t region, std::uint64_t frame,
const auto record = archive.organism(corpse.organism);
if (!record || record->alive || record->died.value != corpse.died_tick)
return std::unexpected{CellularLivesError::archive};
const auto genome = archive.find(record->genome);
const auto current_genome = archive.current_genome(corpse.organism);
if (!current_genome)
return std::unexpected{CellularLivesError::archive};
const auto genome = archive.find(*current_genome);
if (!genome)
return std::unexpected{CellularLivesError::archive};
const auto budget = biology::metabolic_budget(*genome, corpse.growth,
@@ -347,13 +352,13 @@ CellularLives::create(const PrimitiveLives& opening,
opening.balances()[body_index],
opening.balances()[reserve_index]});
}
auto archive = opening.archive();
std::ranges::sort(lives, {}, [](const auto& life) { return life.identity.life; });
for (std::size_t i = 0; i < lives.size(); ++i) {
const auto& life = lives[i];
if (i != 0 && lives[i - 1].identity.life == life.identity.life)
return std::unexpected{CellularLivesError::duplicate_life};
const auto budget =
budget_for(opening.archive(), life, config.ambient_temperature_millikelvin);
const auto budget = budget_for(archive, life, config.ambient_temperature_millikelvin);
if (!budget || budget->phenotype.extent == 0 ||
budget->phenotype.extent > config.area.width_um ||
budget->phenotype.extent > config.area.height_um || life.body.energy.value != 0 ||
@@ -368,9 +373,9 @@ CellularLives::create(const PrimitiveLives& opening,
const auto nursery = opening.balances()[0];
nursery_config.funding = nursery;
const auto heat = opening.balances()[1];
const auto current = validate_state(nursery_config.world, nursery_config.region,
nursery_config.frame, config, nursery_config, *habitat,
lives, {}, opening.archive(), nursery, heat, opening_total);
const auto current =
validate_state(nursery_config.world, nursery_config.region, nursery_config.frame, config,
nursery_config, *habitat, lives, {}, archive, nursery, heat, opening_total);
if (!current)
return std::unexpected{current.error()};
@@ -379,8 +384,8 @@ CellularLives::create(const PrimitiveLives& opening,
const auto frame = nursery_config.frame;
auto state = std::make_unique<State>(world, region, frame, config, std::move(nursery_config),
std::move(habitat), std::move(lives),
std::vector<CellularOwnedCorpse>{}, opening.archive(),
nursery, heat, opening_total, *current);
std::vector<CellularOwnedCorpse>{}, archive, nursery, heat,
opening_total, *current);
return std::unique_ptr<CellularLives>{new CellularLives{std::move(state)}};
}
@@ -458,8 +463,11 @@ std::vector<std::uint8_t> CellularLives::snapshot() const {
std::vector<std::uint8_t> bytes;
bytes.reserve(total_bytes);
Writer out{bytes};
out.put(state_->format_version == cellular_lives_legacy_format ? cellular_lives_v1_magic
: cellular_lives_v2_magic);
const auto magic =
state_->format_version == cellular_lives_legacy_format ? cellular_lives_v1_magic
: state_->format_version == cellular_lives_revision_format ? cellular_lives_v3_magic
: cellular_lives_v2_magic;
out.put(magic);
out.put(state_->format_version, 4);
out.put(state_->rules_version, 4);
out.put(state_->world);
@@ -595,6 +603,32 @@ CellularLives::migrate_to_v2() const {
return candidate;
}
std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
CellularLives::migrate_to_v3() const {
if (state_->format_version != cellular_lives_format ||
state_->rules_version != cellular_lives_rules ||
state_->archive.format_version() != biology::lineage_archive_format)
return std::unexpected{CellularLivesError::version};
auto archive = state_->archive;
archive.upgrade_to_revision_format();
auto habitat = biology::CellularHabitat::restore(state_->habitat->snapshot(), state_->world,
state_->region, state_->frame);
if (!habitat)
return std::unexpected{CellularLivesError::habitat};
const auto current =
validate_state(state_->world, state_->region, state_->frame, state_->config,
state_->nursery_config, **habitat, state_->lives, state_->corpses, archive,
state_->nursery, state_->heat, state_->opening_total);
if (!current || *current != state_->current_total)
return std::unexpected{current ? CellularLivesError::conservation : current.error()};
auto candidate = std::unique_ptr<CellularLives>{new CellularLives{std::make_unique<State>(
state_->world, state_->region, state_->frame, state_->config, state_->nursery_config,
std::move(*habitat), state_->lives, state_->corpses, archive, state_->nursery, state_->heat,
state_->opening_total, state_->current_total, cellular_lives_revision_format,
cellular_lives_rules)}};
return candidate;
}
std::expected<std::unique_ptr<CellularLives>, CellularLivesError>
CellularLives::birth_candidate(LifeRequest request, LifeAllocation allocation) const {
if (request.action == 0 ||
@@ -723,10 +757,14 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
const auto rules_version = static_cast<std::uint32_t>(in.get(4));
const bool legacy = magic == cellular_lives_v1_magic &&
format_version == cellular_lives_legacy_format && rules_version == 1;
const bool current_version = magic == cellular_lives_v2_magic &&
format_version == cellular_lives_format &&
rules_version == cellular_lives_rules;
if (!legacy && !current_version)
const bool offer_version = magic == cellular_lives_v2_magic &&
format_version == cellular_lives_format &&
rules_version == cellular_lives_rules;
const bool revision_version = magic == cellular_lives_v3_magic &&
format_version == cellular_lives_revision_format &&
rules_version == cellular_lives_rules;
const bool mutation_version = offer_version || revision_version;
if (!legacy && !mutation_version)
return std::unexpected{CellularLivesError::version};
const auto saved_world = in.get();
const auto saved_region = in.get();
@@ -785,7 +823,7 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
life.position = {in.get(), in.get()};
life.body = in.inventory();
life.reserve = in.inventory();
if (current_version) {
if (mutation_version) {
auto& mutation = life.mutation;
mutation.fed_energy_credit_pj = in.get();
mutation.event_index = in.get();
@@ -873,7 +911,10 @@ CellularLives::restore(std::span<const std::uint8_t> bytes, std::uint64_t world,
return std::unexpected{CellularLivesError::noncanonical};
return std::unexpected{CellularLivesError::archive};
}
if (current_version) {
if (archive->format_version() != (revision_version ? biology::lineage_archive_revision_format
: biology::lineage_archive_format))
return std::unexpected{CellularLivesError::version};
if (mutation_version) {
for (const auto& life : lives) {
if (!life.mutation.offer)
continue;